* Kristoffer Balle Hvidberg
* Field of Study and Financial Problems: How Economics Reduces the Risk of Default

********************************************************************************

* PART 1: Generate analyse_del_3 data set
* PART 2: Generate analyse_first_time_applicants data set
* PART 3: Generate mean_restance_udd_yoa and add to analyse_del_3 along with institutions

********************************************************************************

clear all
set more off

global data "K:\Workdata\704365\kbh\FSFP_Replication_Package\data"
global temp "K:\Workdata\704365\kbh\FSFP_Replication_Package\data\temp"
global raw  "K:\Workdata\704365\kbh\FSFP_Replication_Package\data\raw"

********************************************************************************
		
**********
* PART 1 *
**********

* KOT APPLICANTS

	* Load registerdata
	use "$temp\registerdata", clear
	drop if mi(year)
	
	merge 1:1 pnr year using "$temp/inc_rank"
	drop if _merge==2
	drop _merge
	merge m:1 pnr using "$temp/inc_rank_age"
	drop if _merge==2
	drop _merge
	
	* Age
	gen birthyear_temp=year(foed_dag)
	bysort pnr: egen birthyear = mean(birthyear_temp)
	count if birthyear!=birthyear_temp & !mi(birthyear_temp)
	drop birthyear_temp
	gen age=year-birthyear
	replace age=. if age<0
	label var age "Age"

	* Gender
	gen sex_temp=.
	replace sex_temp=1 if koen==1
	replace sex_temp=0 if koen==2
	bysort pnr: egen sex=mean(sex_temp)
	tab sex
	replace sex=. if (sex!=0 & sex!=1)
	drop koen sex_temp

	* Deflate nominal variables
	merge m:1 year using "$raw\cpi.dta"
	drop if _merge==2
	drop _merge

	local inc "bankakt bankgaeld dispon_13 ejendomsvurdering erhvervsindk_13 fosfufrd gaeld_til_off indestpi koejd kursakt loenmv_13 oblakt oblgaeld pantakt pantgaeld perindkialt_13  qarbpenk qarbpenl qpripen qpripenk qpripenl"
	foreach i in `inc' {
		gen `i'_2015p=(`i'/cpi)*100
		local lbl: variable label `i'
		label variable `i'_2015p "`lbl' (2015 priser)"
	}

	save "$temp\registerdata_edit1"

* FAMILY 

	use "$temp\registerdata_fam",clear

	merge 1:1 pnr year using "$temp/inc_rank"
	drop if _merge==2
	drop _merge
	merge m:1 pnr using "$temp/inc_rank_age"
	drop if _merge==2
	drop _merge

	* Age
	gen birthyear_temp=year(foed_dag)
	bysort pnr: egen birthyear = mean(birthyear_temp)
	count if birthyear!=birthyear_temp & !mi(birthyear_temp)
	drop birthyear_temp
	gen age=year-birthyear
	replace age=. if age<0
	label var age "Age"

	* Deflate nominal variables
	merge m:1 year using "$raw\cpi.dta"
	drop if _merge==2
	drop _merge

	local inc "dispon_13 erhvervsindk_13 perindkialt_13"
	foreach i in `inc' {
		gen `i'_2015p=(`i'/cpi)*100
		local lbl: variable label `i'
		label variable `i'_2015p "`lbl' (2015 priser)"
	}

	* Keep relevant variables and sort
	keep pnr year birthyear age erhvervsindk_13_2015p perindkialt_13_2015p hfaudd inc*
	sort pnr year

	* Compute avg. income (labor and total) age 40-44
	by pnr: egen labor_inc_4044_temp=mean(erhvervsindk_13_2015p) if age>39 & age<45
	by pnr: egen labor_inc_4044=mean(labor_inc_4044_temp) 

	by pnr: egen total_inc_4044_temp=mean(perindkialt_13_2015p) if age>39 & age<45
	by pnr: egen total_inc_4044=mean(total_inc_4044_temp)

	drop labor_inc_4044_temp total_inc_4044_temp

	* Add highest completed educatopn
	tostring hfaudd, gen(audd) format(%04.0f)
	replace audd="" if audd=="."

	sort audd

	merge m:1 audd using "$temp\audd_niveau_field"
	drop if _merge==2
	drop _merge

	drop hfaudd audd_navn

	save "$temp\registerdata_temp"

* Partner
	merge m:1 audd using "$temp\link_audd_stem"
	drop if _merge==2
	drop _merge

	* Fields of study
	gen audd_n = 0 
	replace audd_n = 1  if stem == 1
	replace audd_n = 2  if oeko == 1 
	replace audd_n = 3  if jura == 1
	replace audd_n = 4  if huaa == 1
	replace audd_n = 5  if educ == 1
	replace audd_n = 6  if welf == 1
	replace audd_n = 7  if medi == 1
	replace audd_n = 8  if sosc == 1

	rename audd_field4 disced_audd

	drop stem oeko jura huaa educ welf medi sosc audd_field2 audd_field3

	* Average income across 3 years
	sort pnr year

	gen erhvervsindk = (erhvervsindk_13_2015p + erhvervsindk_13_2015p[_n-1] + erhvervsindk_13_2015p[_n+1])/3 if pnr==pnr[_n-1] & pnr==pnr[_n+1] & year==year[_n-1]+1 & year==year[_n+1]-1 & !mi(erhvervsindk_13_2015p) & !mi(erhvervsindk_13_2015p[_n-1]) & !mi(erhvervsindk_13_2015p[_n+1])

	gen perindkialt = (perindkialt_13_2015p + perindkialt_13_2015p[_n-1] + perindkialt_13_2015p[_n+1])/3 if pnr==pnr[_n-1] & pnr==pnr[_n+1] & year==year[_n-1]+1 & year==year[_n+1]-1 & !mi(perindkialt_13_2015p) & !mi(perindkialt_13_2015p[_n-1]) & !mi(perindkialt_13_2015p[_n+1])

	* Rename variables and save
	rename pnr efalle
	keep efalle year erhvervsindk_13_2015p erhvervsindk perindkialt_13_2015p perindkialt audd audd_niveau disced_audd audd_n inc_rank inc_rank_35 inc_rank_40 inc_rank_45
	foreach x in erhvervsindk_13_2015p erhvervsindk perindkialt_13_2015p perindkialt audd audd_niveau disced_audd audd_n inc_rank inc_rank_35 inc_rank_40 inc_rank_45 {
		rename `x' `x'_efalle
	}
	save "$temp\registerdata_efalle"

* Father
	use "$temp\registerdata_temp", clear

	keep pnr year labor_inc_4044 total_inc_4044 audd audd_niveau  inc_rank_35 inc_rank_40 inc_rank_45

	sort pnr year

	replace audd=audd[_n-1] if mi(audd) & pnr==pnr[_n-1]
	replace audd_niveau=audd_niveau[_n-1] if mi(audd_niveau) & pnr==pnr[_n-1]

	gsort pnr - year

	replace audd=audd[_n-1] if pnr==pnr[_n-1]
	replace audd_niveau=audd_niveau[_n-1] if pnr==pnr[_n-1]

	sort pnr year

	rename pnr far_id

	drop year

	duplicates drop
	duplicates report far_id
	foreach x in labor_inc_4044 total_inc_4044 audd audd_niveau inc_rank_35 inc_rank_40 inc_rank_45 {
		rename `x' `x'_far
	}
	save "$temp\registerdata_far"

* Mother
	rename far_id mor_id

	foreach x in labor_inc_4044 total_inc_4044 audd audd_niveau  inc_rank_35 inc_rank_40 inc_rank_45 {
		rename `x'_far `x'_mor
	}

	save "$temp\registerdata_mor"


* MERGE	

	use "$temp\registerdata_edit1", clear
	sort efalle year
	duplicates report efalle year if !mi(efalle)
	merge m:1 efalle year using "$temp\registerdata_efalle"
	drop if _merge==2
	drop _merge
	merge m:1 efalle year using "$raw\urte_efalle_edit"
	drop if _merge==2
	drop _merge

	sort far_id year
	merge m:1 far_id using "$temp\registerdata_far"
	drop if _merge==2
	drop _merge

	sort mor_id year
	merge m:1 mor_id using "$temp\registerdata_mor"
	drop if _merge==2
	drop _merge

	sort pnr year

	save "$temp\registerdata_edit2"

********************************************************************************

* AGE AND PREVIOUS EDUCATION AT YEAR OF APPLICATION (YOA)

	use "$raw\kot2016", clear
	sort pnr aar PRIO_NR
	rename aar yoa
	drop if yoa>2006
	drop if mi(pnr)
	drop if mi(yoa)
	destring pnr, gen(pnr_num) force
	drop if mi(pnr_num)
	drop pnr_num
	drop if afmeldtjn==1
	destring PRIO_NR, replace
	sort pnr yoa PRIO_NR
	drop if PRIO_NR>8
	sort pnr
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 531,033 */
	drop t

	keep pnr yoa
	sort pnr yoa
	drop if pnr==pnr[_n-1]
	merge 1:m pnr using "$temp\registerdata_edit2"
	keep if _merge==3
	drop _merge

	keep pnr year age hfaudd udd yoa
	sort udd 
	merge m:1 udd using "$temp\udd_niveau_field"
	drop if _merge==2
	drop _merge
	tostring hfaudd, gen(audd) format(%04.0f)
	replace audd = "" if audd=="."
	sort audd
	merge m:1 audd using "$temp\audd_niveau_field"
	drop if _merge==2
	drop _merge
	drop udd_navn udd_field* audd_navn audd_field*

	* Indicate level of current education
	tab udd_niveau
	gen tidligere_vu = 1 if year<yoa & ( ///
		udd_niveau=="Ph.d. og forskeruddannelser"  		| ///
		udd_niveau=="Korte videregående uddannelser"  	| ///
		udd_niveau=="Lange videregående uddannelser"  	| ///
		udd_niveau=="Mellemlange videregående uddannelser / Bachelorer")
		sort pnr year
	by pnr: egen vu1 = max(tidligere_vu)
	drop udd_niveau tidligere_vu
	label var vu1 "=1 if enrolled in higher education prior to YOA"
	
	* Indicate level of previosly completed education
	tab audd_niveau
	gen tidligere_vu = 1 if year<=yoa & ( ///
		audd_niveau=="Ph.d. og forskeruddannelser"  	| ///
		audd_niveau=="Korte videregående uddannelser"  	| ///
		audd_niveau=="Lange videregående uddannelser"  	| ///
		audd_niveau=="Mellemlange videregående uddannelser / Bachelorer")
		sort pnr year
	by pnr: egen vu2 = max(tidligere_vu)
	drop audd_niveau tidligere_vu 
	label var vu2 "=1 if completed higher education prior to YOA"
	
	* Indicate age at YOA
	gen age_yoa=age if year==yoa	
	gen age1830_temp=1 if inrange(age_yoa,18,30) & !mi(age_yoa)
	by pnr: egen age1830=max(age1830_temp)

	keep pnr vu1 vu2 age1830
	duplicates drop
	duplicates report pnr

	save "$temp\vu_for_yoa"

********************************************************************************

* PREPARE APPLICATION AND ACCEPTANCE DATA
	
	use "$raw\kot2016", clear

	sort pnr aar PRIO_NR
	rename aar year

	* Keep relevant observations
	drop if year>2006
	drop if mi(pnr)
	destring pnr, gen(pnr_num) force
	drop if mi(pnr_num)
	drop pnr_num
	drop if afmeldtjn==1 // Cancelled applications
	destring PRIO_NR, replace
	sort pnr year PRIO_NR
	drop if PRIO_NR>8

	* Add GPA thresholds and applicants' GPAs
	destring kotnr, replace
	sort kotnr year
	merge m:1 kotnr year using "$raw\kot_adgangskvotienter"
	drop if _merge==2 
	rename _merge adgangskvotienter_merge
	sort pnr year 
	merge m:1 pnr year using "$raw\udg_edit"
	drop if _merge==2
	rename _merge udg_merge

	* Rename
	gen double gpa=karakter_udd/10
	drop karakter_udd
	rename adgangskvotient cut
	rename optagetjn optaget
	rename optagetstjn optagetst
	rename OPTKV1JN optkv1
	rename OPTKV1STJN  optkv1st
	rename OPTKV2JN optkv2
	rename OPTKV2STJN optkv2st
	rename SOEGKV1JN soegkv1
	rename SOEGKV1STJN soegkv1st
	rename SOEGKV2JN soegkv2
	rename SOEGKV2STJN soegkv2st
	rename PRIO_NR prioritet
	
	* Distance to threshold and indicator for being above
	gen dist=gpa-cut
	gen over=.
	replace over=1 if dist>=0 & !mi(dist)
	replace over=0 if dist<0 & !mi(dist)
	
	* Order
	order pnr year prioritet optaget optagetst cut gpa dist over optkv1 optkv1st optkv2 optkv2st soegkv1 soegkv1st soegkv2 soegkv2st

	* Keep observations from first year of application
	bysort pnr: egen temp=min(year)
	drop if year!=temp
	drop temp
	count if pnr==pnr[_n-1] & year!=year[_n-1]
	sort pnr 
	gen t=1 if (pnr!=pnr[_n-1]) 
	count if !mi(t) /* 531,033 */
	drop t
	
	merge m:1 pnr using "$temp\vu_for_yoa"
	drop if _merge==2
	
	drop if vu1==1 | vu2==1 // 81,671 observations deleted)
	keep if age1830==1 // 79,761 observations deleted 
	
	drop _merge vu1 vu2 age1830
	
	sort pnr year 
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 427,885 */
	drop t

	* Save a data set with all first time applicants for descriptive statistics
	save "$temp\kot2016_temp1"

	keep pnr year prioritet optaget gpa cut kotnr kot_navn
	bysort pnr year: egen temp = sum(optaget)
	gen kotnr_opt_temp = kotnr if optaget==1
	bysort pnr year: egen kotnr_opt = max(kotnr_opt_temp) if temp==1
	gen kot_navn_opt = kot_navn if optaget==1 & temp==1
	gsort pnr year - optaget
	replace kot_navn_opt=kot_navn_opt[_n-1] if pnr==pnr[_n-1] & year==year[_n-1] & !mi(kot_navn_opt[_n-1])
	drop temp kotnr_opt_temp kotnr kot_navn
	gen opt_prio_temp=prioritet if optaget==1
	destring opt_prio_temp, replace
	bysort pnr year: egen opt_prio=mean(opt_prio_temp)
	bysort pnr year: egen opt_temp=max(optaget)
	gen no_offer=(opt_temp==0)
	gen cut_first_temp = cut if prioritet==1
	bysort pnr year: egen cut_first=mean(cut_first_temp)
	drop optaget prioritet opt_temp opt_prio_temp cut cut_first_temp
	gen num=1
	by pnr year, sort: egen num_of_app=sum(num)
	drop num
	duplicates drop
	duplicates report pnr year opt_prio
	sort pnr year opt_prio
	drop if pnr==pnr[_n-1] & year==year[_n-1]
	duplicates report pnr year
	save "$temp\kot_population"

	use "$temp\kot2016_temp1", clear	
	
	* Save a data set with pnr's on all first time applicants
	keep pnr 
	duplicates drop
	
	save "$temp\pnr_first_time_applicants"
	
	use "$temp\kot2016_temp1", clear	
	
	* Sort
	duplicates report pnr prioritet cut kotnr
	sort pnr prioritet cut kotnr
	
	* Drop if several programs have first priority
	gen temp1 = 1 if prioritet==1 & prioritet[_n+1]==1 & pnr==pnr[_n+1]
	by pnr: egen temp2=max(temp1)
	drop if temp2==1
	drop temp1 temp2
	
	sort pnr
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 426,666 */
	drop t	

	* Drop individual if...
	
	* ... we observe less than two applications in the ordinary system
	gen ansogning=1 if !mi(cut)
	bysort pnr year: egen antal_ans=sum(ansogning)
	drop if antal_ans<2
	drop ansogning antal_ans

	sort pnr
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 170,098 */
	drop t

	* ... unrestricted access to first priority program
	gen temp1=1 if prioritet==1 & cut==0
	by pnr: egen temp2=max(temp1)
	drop if temp2==1
	drop temp1 temp2
	
	sort pnr
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 133,009 */
	drop t
	
	* ... accepted at program with a non-standard admission procedure
	gen temp1=1 if optaget==1 & mi(cut)
	bysort pnr year: egen temp2=max(temp1)
	drop if temp2==1
	drop temp1 temp2
	
	sort pnr 
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 129,320 */
	drop t
	
	* ... not accepted at any program
	bysort pnr year: egen temp=sum(optaget)
	drop if temp==0
	
	* ... accepted at several programs
	drop if temp>1
	drop temp
	
	sort pnr 
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 98,688 */
	drop t
	
	* ... gpa is missing 
	drop if mi(gpa)

	sort pnr
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 81,350 */
	drop t
	
	* ... gpa lower than threshold for all programs
	by pnr: egen temp=sum(over) 
	drop if temp==0
	drop temp
	
	sort pnr 
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 67,522 */
	drop t
		
	* Indicate what the priority was for the program of acceptance
	gen opt_prio_temp=prioritet if optaget==1
	destring opt_prio_temp, replace
	bysort pnr year: egen opt_prio=mean(opt_prio_temp)
	drop opt_prio_temp
	bysort pnr year: egen opt_kv2=max(optkv2)

	
	* Delete application if...

	* ... dublettes i priority

	duplicates report pnr prioritet
	duplicates tag pnr prioritet, gen(dup)
	drop if dup>0
	drop dup 
	duplicates report pnr prioritet 
	sort pnr prioritet

	* ... program has a non-standard system of admission or is not formed
	drop if cut==.

	* ... higher priority program with a lower threshold
	sort pnr prioritet
	drop if cut>=cut[_n-1] & pnr==pnr[_n-1] 
	drop if cut>=cut[_n-1] & pnr==pnr[_n-1] 
	drop if cut>=cut[_n-1] & pnr==pnr[_n-1] 
	drop if cut>=cut[_n-1] & pnr==pnr[_n-1] 
	drop if cut>=cut[_n-1] & pnr==pnr[_n-1] 
	drop if cut>=cut[_n-1] & pnr==pnr[_n-1] 

	* ... if the applicant's gpa is below the threshold for this priority and the next priority program
	drop if over==0 & over[_n+1]==0 & pnr==pnr[_n+1]
	drop if over==0 & over[_n+1]==0 & pnr==pnr[_n+1]

	* ... gpa is above threshold for two programs with higher priority
	drop if over[_n-1]==1 & over[_n-2]==1 & pnr==pnr[_n-1] & pnr==pnr[_n-2]
	drop if over[_n-1]==1 & over[_n-2]==1 & pnr==pnr[_n-1] & pnr==pnr[_n-2]

	* Keep applicant if there is more than one application left
	gen temp1=1
	bysort pnr: egen temp2=sum(temp1)
	count if temp2==1
	drop if temp2==1
	drop temp1 temp2
	
	sort pnr year prioritet
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 53,882 */
	drop t

	* Check that there is only two observations per applicant unless the pattern is 0-1-1 in over
	gen tal=1
	bysort pnr: egen antal=sum(tal)
	tab antal
	gen temp=1 if pnr!=pnr[_n-1] & antal==3
	count if temp==1 & over!=0
	drop tal temp
	
	* Check that all applicants are above the threshold for at least one of the remaining programs
	bysort pnr year: egen temp=sum(over)
	tab temp
	drop temp

	* Drop the "third observation"
	duplicates report pnr 
	duplicates report pnr prioritet
	sort pnr prioritet
	drop if pnr==pnr[_n-1] & pnr==pnr[_n-2]
	
	sort pnr prioritet
	gen t=1 if pnr!=pnr[_n-1]
	count if !mi(t) /* 53,882 */
	drop t

	* Keep relevant variables
	keep pnr year prioritet optaget optagetst cut gpa dist kotnr kot_navn optkv1 optkv1st optkv2 optkv2st soegkv1 soegkv1st soegkv2 soegkv2st opt_prio opt_kv2

	* Generate cross-section structure
	rename kotnr 		kotnr1 
	rename kot_navn 	kotnavn1
	rename optaget 		optaget1
	rename optagetst 	optagetst1
	rename optkv1 		optkv1_1 
	rename optkv1st 	optkv1st_1 
	rename optkv2 		optkv2_1 
	rename optkv2st 	optkv2st_1
	rename soegkv1 		soegkv1_1 
	rename soegkv1st 	soegkv1st_1
	rename soegkv2 		soegkv2_1
	rename soegkv2st 	soegkv2st_1
	rename prioritet	prioritet_1

	sort pnr prioritet_1

	gen kotnr2		=kotnr1[_n+1] 		if pnr==pnr[_n+1] 
	gen kotnavn2	=kotnavn1[_n+1] 	if pnr==pnr[_n+1] 
	gen optaget2	=optaget1[_n+1] 	if pnr==pnr[_n+1] 
	gen optagetst2	=optagetst1[_n+1]	if pnr==pnr[_n+1] 
	gen optkv1_2	=optkv1_1[_n+1] 	if pnr==pnr[_n+1] 
	gen optkv1st_2	=optkv1st_1[_n+1] 	if pnr==pnr[_n+1] 
	gen optkv2_2	=optkv2_1[_n+1] 	if pnr==pnr[_n+1] 
	gen optkv2st_2	=optkv2st_1[_n+1]	if pnr==pnr[_n+1] 
	gen soegkv1_2 	=soegkv1_1[_n+1]	if pnr==pnr[_n+1]  	
	gen soegkv1st_2	=soegkv1st_1[_n+1]	if pnr==pnr[_n+1] 
	gen soegkv2_2 	=soegkv2_1[_n+1]	if pnr==pnr[_n+1] 
	gen soegkv2st_2	=soegkv2st_1[_n+1]	if pnr==pnr[_n+1] 
	gen prioritet_2 =prioritet_1[_n+1]  if pnr==pnr[_n+1]

	drop if mi(kotnr2)
	duplicates report pnr

	* Labels
	label var gpa 			"Karaktergennemsnit"
	label var kotnr1 		"Den foretrukne uddannelses KOT-nummer"
	label var kotnavn1 		"Den foretrukne uddannelses navn"
	label var kotnr2 		"Den alternative uddannelses KOT-nummer"
	label var kotnavn2 		"Den alternative uddannelses navn"
	label var optaget1 		"Angiver om personen er blevet optaget på den foretrukne uddannelse"
	label var optaget2 		"Angiver om personen er blevet optaget på den alternative uddannelse"
	label var optagetst1	"Angiver om personen er blevet optaget på standby på den foretrukne uddannelse"
	label var optagetst2	"Angiver om personen er blevet optaget på standby på den alternative uddannelse"
	label var cut 			"Adgangskvotient på den foretrukne uddannelse"
	label var dist 			"Forskel mellem karaktergennemsnit og adgangskvotient på den foretrukne uddannelse"
	label var optkv1_1		"Angiver om personen er blevet optaget på den foretrukne uddannelse gennem kvote 1" 
	label var optkv1st_1 	"Angiver om personen er blevet optaget på den foretrukne uddannelse gennem kvote 1, standby"
	label var optkv2_1 		"Angiver om personen er blevet optaget på den foretrukne uddannelse gennem kvote 2"
	label var optkv2st_1 	"Angiver om personen er blevet optaget på den foretrukne uddannelse gennem kvote 2, standby"
	label var optkv1_2 		"Angiver om personen er blevet optaget på den alternative uddannelse gennem kvote 1" 
	label var optkv1st_2 	"Angiver om personen er blevet optaget på den alternative uddannelse gennem kvote 1, standby"
	label var optkv2_2 		"Angiver om personen er blevet optaget på den alternative uddannelse gennem kvote 2"
	label var optkv2st_2	"Angiver om personen er blevet optaget på den alternative uddannelse gennem kvote 2, standby"
	label var soegkv1_1		"Angiver om personen har søgt den foretrukne uddannelse gennem kvote 1" 
	label var soegkv1st_1   "Angiver om personen har søgt den foretrukne uddannelse gennem kvote 1, standby"
	label var soegkv2_1     "Angiver om personen har søgt den foretrukne uddannelse gennem kvote 2"
	label var soegkv2st_1   "Angiver om personen har søgt den foretrukne uddannelse gennem kvote 2, standby"
	label var soegkv1_2     "Angiver om personen har søgt den alternative uddannelse gennem kvote 1" 
	label var soegkv1st_2   "Angiver om personen har søgt den alternative uddannelse gennem kvote 1, standby"
	label var soegkv2_2     "Angiver om personen har søgt den alternative uddannelse gennem kvote 2"
	label var soegkv2st_2   "Angiver om personen har søgt den alternative uddannelse gennem kvote 2, standby"
	label var prioritet_1	"Angiver prioriteten for den foretrukne uddannelse"
	label var prioritet_2	"Angiver prioriteten for den alternative uddannelse"
	label var opt_prio 		"Angiver hvilken prioritet ansøgeren er optaget på"
	label var opt_kv2		"Angiver om ansøgeren er optaget gennem kvote 2"
	

	* Sort and save
	sort pnr year prioritet_1
	save "$temp\optagelse"

	* Merge with registerdata
	merge m:1 pnr year using "$temp\registerdata_edit2"

	* Keep applicants in optagelse
	gen temp1=(_merge==3)
	bysort pnr: egen temp2=max(temp1)
	keep if temp2==1
	drop temp1 temp2 _merge

	* Indicate level for current and highest completed education
	tostring udd_n, replace format(%04.0f)
	replace udd_n="" if udd_n=="."
	replace udd = udd_n if mi(udd)
	sort udd
	merge m:1 udd using "$temp\udd_niveau_field"
	drop if _merge==2
	drop _merge
	drop audd
	tostring hfaudd, gen(audd) format(%04.0f)
	label var audd "Highest completed education"
	sort audd
	merge m:1 audd using "$temp\audd_niveau_field"
	drop if _merge==2
	drop _merge
	tab audd_niveau if !mi(prioritet_1)
	sort pnr year
	drop udd_n hfaudd
	
	* Order
	order pnr year age sex prioritet_1 optaget1 prioritet_2 optaget2 cut gpa dist kotnr1 kotnavn1 kotnr2 kotnavn2 udd audd

	* Add classifications
	sort kotnr1 year 
	merge m:1 kotnr1 year using "$temp\kotnr1"
	drop if _merge==2
	drop _merge
	sort kotnr2 year 
	merge m:1 kotnr2 year using "$temp\kotnr2"
	drop if _merge==2
	drop _merge
	
	sort audd
	merge m:1 audd using "$temp\link_audd_stem"
	drop if _merge==2
	drop _merge
	merge m:1 audd using "$temp\link_audd_econ"
	drop if _merge==2
	drop _merge
	
	replace audd="" if audd=="."
	
	foreach x in stem oeko jura huaa educ welf medi sosc econ {
		rename `x' audd_`x'
	}
	
	sort udd 
	merge m:1 udd using "$temp\link_udd_stem"
	drop if _merge==2
	drop _merge
	merge m:1 udd using "$temp\link_udd_econ"
	drop if _merge==2
	drop _merge
	
	replace udd="" if udd=="."
	
	foreach x in stem oeko jura huaa educ welf medi sosc econ {
		rename `x' udd_`x'
	}

	* Sort and count
	sort pnr year
	gen t=1 if pnr!=pnr[_n-1] 
	count if !mi(t) /* 53,882 */
	drop t

	* Year of application
	gen yoa_temp=year if !mi(gpa)
	bysort pnr: egen yoa=max(yoa_temp)
	drop yoa_temp
	label var yoa "Year of application"
	
	* Years since application
	gen time=year-yoa
	label var time "Years since application"
	
	* Save
	save "$temp\analyse"

********************************************************************************

	* Add IRTE data
	merge m:1 pnr year using "$raw\irte_edit"
	drop if _merge==2
	gen in_irte=(_merge==3)
	drop _merge

	* Age at application
	gen birth_year = year(foed_dag)
	gen birth_month = month(foed_dag)
	gen age_application = ym(birth_year,birth_month)
	format age_application %tmNN/YY
	sort pnr age_application 
	replace age_application=age_application[_n-1] if mi(age_application) & pnr==pnr[_n-1]

	* Sort and count
	sort pnr year
	gen t=1 if pnr!=pnr[_n-1] 
	count if !mi(t) /* 53,882 */
	drop t

	* Dummy=1, if above threshold of preferred program
	gen over=(dist>=0) if !mi(dist)
	label var over "Above threshold"
	* Dummy=1, if below threshold of preferred program
	gen under=(dist<0) if !mi(dist)
	label var under "Below threshold"

	* Highest completed education in the year of education
	gen audd_0ya = audd if time==0
	
	* Current education
	forval x = 0/4 {
		foreach y in stem oeko jura huaa educ welf medi sosc {
			gen `y'_`x'ya = udd_`y' if time==`x'
			replace `y'_`x'ya = `y'_`x'ya[_n-1] if mi(`y'_`x'ya) & pnr==pnr[_n-1]
		}
		foreach y in udd udd_field4 {
			gen `y'_`x'ya = `y' if time==`x'
			replace `y'_`x'ya = `y'_`x'ya[_n-1] if mi(`y'_`x'ya) & pnr==pnr[_n-1]
		}
	}
	
	* Highest completed education
	forval x = 5/10 {
		foreach y in stem oeko jura huaa educ welf medi sosc {
			gen `y'_`x'ya = audd_`y' if time==`x'
			replace `y'_`x'ya = `y'_`x'ya[_n-1] if mi(`y'_`x'ya) & pnr==pnr[_n-1]
		}
		foreach y in audd audd_field4 {
			gen `y'_`x'ya = `y' if time==`x'
			replace `y'_`x'ya = `y'_`x'ya[_n-1] if mi(`y'_`x'ya) & pnr==pnr[_n-1]
		}
	}
	
	gsort pnr - year
	
	forval x = 0/4 {
		foreach y in stem oeko jura huaa educ welf medi sosc udd udd_field4 {
			replace `y'_`x'ya = `y'_`x'ya[_n-1] if mi(`y'_`x'ya) & pnr==pnr[_n-1]
		}
	}
	
	forval x = 5/10 {
		foreach y in stem oeko jura huaa educ welf medi sosc audd audd_field4 {
			replace `y'_`x'ya = `y'_`x'ya[_n-1] if mi(`y'_`x'ya) & pnr==pnr[_n-1]
		}
	}
	
	sort pnr year
	
	forval x = 5/10 {
		gen 	kand_`x'ya = 0 if time == `x' & !mi(audd_niveau)
		replace kand_`x'ya = 1 if time == `x' & audd_niveau=="Lange videregående uddannelser"
		gen 	bach_`x'ya = 0 if time == `x' & !mi(audd_niveau)
		replace bach_`x'ya = 1 if time == `x' & audd_niveau=="Mellemlange videregående uddannelser / Bachelorer"
		
		replace kand_`x'ya = kand_`x'ya[_n-1] if mi(kand_`x'ya) & pnr==pnr[_n-1]
		replace bach_`x'ya = bach_`x'ya[_n-1] if mi(bach_`x'ya) & pnr==pnr[_n-1]
	}
	
	gen audd_niveau_10ya = audd_niveau if time==10
	replace audd_niveau_10ya = audd_niveau_10ya[_n-1] if mi(audd_niveau_10ya) & pnr==pnr[_n-1]
	
	gsort pnr - year
	
	forval x = 5/10 {
		replace kand_`x'ya = kand_`x'ya[_n-1] if mi(kand_`x'ya) & pnr==pnr[_n-1]
		replace bach_`x'ya = bach_`x'ya[_n-1] if mi(bach_`x'ya) & pnr==pnr[_n-1]
	}
	
	forval x=6/10 {
		loc i =`x'-1
		replace kand_`x'ya = kand_`i'ya if kand_`x'ya==0 & kand_`i'ya==1
		
	}
	
	replace audd_niveau_10ya = audd_niveau_10ya[_n-1] if mi(audd_niveau_10ya) & pnr==pnr[_n-1]
	
	sort pnr year
	
	* Indicator for current enrollment in education 
	gsort pnr - year
	forval x=0/23 {
		cap drop in_edu_`x'ya
		gen in_edu_`x'ya=(!mi(udd)) if time==`x'
		replace in_edu_`x'ya=in_edu_`x'ya[_n-1] if pnr==pnr[_n-1] & time>=0 & mi(in_edu_`x'ya)
	}
	sort pnr year
	
	* Income rank
	gen inc_rank_9ya_temp  = inc_rank if time==9
	gen inc_rank_10ya_temp = inc_rank if time==10
	gen inc_rank_11ya_temp = inc_rank if time==11
	gen inc_rank_12ya_temp = inc_rank if time==12
	bysort pnr: egen inc_rank_9ya  = max(inc_rank_9ya_temp)
	bysort pnr: egen inc_rank_10ya = max(inc_rank_10ya_temp)
	bysort pnr: egen inc_rank_11ya = max(inc_rank_11ya_temp)
	bysort pnr: egen inc_rank_12ya = max(inc_rank_12ya_temp)
	drop inc_rank inc_rank_9ya_temp inc_rank_10ya_temp inc_rank_11ya_temp inc_rank_12ya_temp
	gen top10_9ya     = (inrange(inc_rank_9ya,91,100))  if !mi(inc_rank_9ya)
	gen bottom50_9ya  = (inrange(inc_rank_9ya,1,50))    if !mi(inc_rank_9ya)
	gen top10_10ya    = (inrange(inc_rank_10ya,91,100)) if !mi(inc_rank_10ya)
	gen bottom50_10ya = (inrange(inc_rank_10ya,1,50))   if !mi(inc_rank_10ya)
	gen top10_11ya    = (inrange(inc_rank_11ya,91,100)) if !mi(inc_rank_11ya)
	gen bottom50_11ya = (inrange(inc_rank_11ya,1,50))   if !mi(inc_rank_11ya)
	
	* Number of children
	gen barn_10ya = (antboernf>0) if !mi(antboernf) & time==10
	gen nbarn_10ya = antboernf if time==10
	replace barn_10ya = barn_10ya[_n-1] if mi(barn_10ya) & pnr==pnr[_n-1]
	replace nbarn_10ya = nbarn_10ya[_n-1] if mi(nbarn_10ya) & pnr==pnr[_n-1]
	gsort pnr - year
	replace barn_10ya = barn_10ya[_n-1] if mi(barn_10ya) & pnr==pnr[_n-1]
	replace nbarn_10ya = nbarn_10ya[_n-1] if mi(nbarn_10ya) & pnr==pnr[_n-1]
	sort pnr year
	
	* Default and delinquency variables
	sum rest* // No observations of 42, 54, and 64
	
	foreach x in 41 44 47 53 54 55 56 57 58 59 60 66 {
		local label: variable label rest`x'
		forval y=2003/2018 {
			gen temp = rest`x' if year==`y'
			bysort pnr: egen rest`x'_`y' = max(temp)
			label var rest`x'_`y' "`label' i `y'"
			drop temp
		}
		forval y=1/25 {
			gen temp=rest`x' if time==`y'
			bysort pnr: egen rest`x'_`y'ya=max(temp)
			label var rest`x'_`y'ya "`label' `y' år efter YOA"
			drop temp
		}
		
	}
	
	egen rest_temp = rowmax(rest41 rest44 rest47 rest53 rest55 rest56 rest57 rest58 rest59 rest60)
	egen blb_temp  = rowtotal(blb_rest41 blb_rest44 blb_rest47 blb_rest53 blb_rest55 blb_rest56 blb_rest57 blb_rest58 blb_rest59 blb_rest60), missing
	
	forval y=1/23 {
		gen temp=rest_temp if time==`y' & year<2017
		bysort pnr: egen rest8_`y'ya=max(temp)
		label var rest8_`y'ya "Restance `y' år efter YOA"
		drop temp
	}
	
	gen temp=blb_temp if year==2016
	bysort pnr: egen blb_rest8_2016=max(temp)
	drop temp
	
	by pnr: egen temp = max(rest_temp) if time>7 & !mi(time) & year<2017
	by pnr: egen rest8_8_all = max(temp)
	label var    rest8_8_all "Restance 8- år efter YOA"
	drop temp
	
	by pnr: egen temp = max(rest_temp) if time>9 & !mi(time) & year<2017
	by pnr: egen rest8_10_all = max(temp)
	label var    rest8_10_all "Restance 10- år efter YOA"
	drop temp
	
	by pnr: egen temp = max(rest_temp) if time>11 & !mi(time) & year<2017
	by pnr: egen rest8_12_all = max(temp)
	label var    rest8_12_all "Restance 12- år efter YOA"
	drop temp
	
	drop rest_temp blb_temp
	
	by pnr: egen temp = max(in_irte) if time>9 & !mi(time) & year<2017
	by pnr: egen in_irte_10_all = max(temp)
	drop temp
	
	by pnr: egen temp = sum(antal_restance) if time>9 & !mi(time) & year<2017
	by pnr: egen rest_total = max(temp)
	label var rest_total "Antal restancer 10- år efter YOA"
	drop temp
	
	egen rest_temp = rowmax(rest41_efalle rest44_efalle rest47_efalle rest53_efalle rest55_efalle rest56_efalle rest57_efalle rest58_efalle rest59_efalle rest60_efalle)
	
	by pnr: egen temp = max(rest_temp) if time>9 & !mi(time) & year<2017
	by pnr: egen rest8_10_all_efalle = max(temp)
	label var    rest8_10_all_efalle "Efalle restance 10- år efter YOA"
	drop temp rest_temp

	* Marginal interest rate
	forval x=10/15 {
	    gen max_rente_`x'ya = max_rente if time==`x' & year<2017
	}
	gsort pnr - time
	forval x=10/15 {
	    replace max_rente_`x'ya = max_rente_`x'ya[_n-1] if pnr==pnr[_n-1] & mi(max_rente_`x'ya)
	}
	sort pnr time
	
	gen temp = max_rente_efalle if time==10
	egen max_rente_efalle_10ya = max(temp)
	drop temp
	
	* Income variables
	by pnr: egen erhvervsindk_1_9ya_temp = mean(erhvervsindk_13_2015p/1000) if time>0 & time<10
	by pnr: egen erhvervsindk_1_9ya = max(erhvervsindk_1_9ya_temp)
	label var    erhvervsindk_1_9ya "Gns. Erhvervsindkomst 1-9 år efter YOA (1000 DKK, 2015 priser)"
	
	by pnr: egen erhvervsindk_9_11ya_temp = mean(erhvervsindk_13_2015p/1000) if time>8 & time<12
	by pnr: egen erhvervsindk_9_11ya = max(erhvervsindk_9_11ya_temp)
	label var    erhvervsindk_9_11ya "Gns. Erhevrvsindkomst 9-11 år efter YOA (1000 DKK, 2015 priser)"
	
	by pnr: egen persindk_1_9ya_temp = mean(perindkialt_13_2015p/1000) if time>0 & time<10
	by pnr: egen persindk_1_9ya = max(persindk_1_9ya_temp)
	label var    persindk_1_9ya "Gns. Personindkomst 1-9 år efter YOA (1000 DKK, 2015 priser)"
	
	drop erhvervsindk_1_9ya_temp erhvervsindk_9_11ya_temp

	by pnr: egen persindk_9_11ya_temp = mean(perindkialt_13_2015p/1000) if time>8 & time<12
	by pnr: egen persindk_9_11ya = max(persindk_9_11ya_temp)
	label var    persindk_9_11ya "Gns. Personindkomst 9-11 år efter YOA (1000 DKK, 2015 priser)"
	
	drop persindk_1_9ya_temp persindk_9_11ya_temp 	
	
	forval x=9/15 {
	    gen persindk_`x'ya     = perindkialt_13_2015p/1000  if time==`x'
		gen erhvervsindk_`x'ya = erhvervsindk_13_2015p/1000 if time==`x'
	}
	gsort pnr - time
	forval x=9/15 {
	    replace persindk_`x'ya     = persindk_`x'ya[_n-1]     if pnr==pnr[_n-1] & mi(persindk_`x'ya)
		replace erhvervsindk_`x'ya = erhvervsindk_`x'ya[_n-1] if pnr==pnr[_n-1] & mi(erhvervsindk_`x'ya)
	}
	sort pnr time
	
	forval x=1993/2016 {
	    gen persindk_`x'     = perindkialt_13_2015p/1000  if year==`x' & time>0
		gen erhvervsindk_`x' = erhvervsindk_13_2015p/1000 if year==`x' & time>0
	}
	gsort pnr - time
	forval x=1993/2016 {
	    replace persindk_`x'     = persindk_`x'[_n-1]     if pnr==pnr[_n-1] & mi(persindk_`x')
		replace erhvervsindk_`x' = erhvervsindk_`x'[_n-1] if pnr==pnr[_n-1] & mi(erhvervsindk_`x')
	}
	sort pnr time
	
	cap drop temp
	by pnr: egen temp = sd(perindkialt_13_2015p/1000) if time>7 & time<13
	by pnr: egen persindk_sd_8_12ya = max(temp)
	label var    persindk_sd_8_12ya "Std. dev. Personindkomst 8-12 år efter YOA (1000 DKK, 2015 priser)"
	
	cap drop temp
	by pnr: egen temp = sd(perindkialt_13_2015p/1000) if time>4 & time<16
	by pnr: egen persindk_sd_5_15ya = max(temp)
	label var    persindk_sd_5_15ya "Std. dev. Personindkomst 5-15 år efter YOA (1000 DKK, 2015 priser)"
	
	* Stock market participation
	forval x = 8/12 {
		gen kursakt_`x'ya = kursakt_2015p if time==`x'
	}
	gsort pnr - time
	forval x = 8/12 {
		replace kursakt_`x'ya = kursakt_`x'ya[_n-1] if !mi(kursakt_`x'ya[_n-1])& pnr==pnr[_n-1]
	}
	sort pnr time

	* DTI ratio
	forval x=10/15 {
	    gen bankgaeld_`x'ya = bankgaeld_2015p/1000 if time==`x'
	}
	gsort pnr - time
	forval x=10/15 {
	    replace bankgaeld_`x'ya = bankgaeld_`x'ya[_n-1] if pnr==pnr[_n-1] & mi(bankgaeld_`x'ya)
	}
	sort pnr time

	* LTI ratio
	gen liquid_assets=kursakt_2015p+oblakt_2015p+pantakt_2015p+bankakt_2015p
	
	forval x=10/15 {
	    gen la_`x'ya = liquid_assets/1000 if time==`x'
	}
	gsort pnr - time
	forval x=10/15 {
	    replace la_`x'ya = la_`x'ya[_n-1] if pnr==pnr[_n-1] & mi(la_`x'ya)
	}
	sort pnr time
	
	forval x=10/15 {
	    gen bankakt_`x'ya = bankakt_2015p/1000 if time==`x'
	}
	gsort pnr - time
	forval x=10/15 {
	    replace bankakt_`x'ya = bankakt_`x'ya[_n-1] if pnr==pnr[_n-1] & mi(bankakt_`x'ya)
	}
	sort pnr time
	
	* Self-employment
	tab psoc_status_kode if time==10
	tab pstill if time==10

	gen 	self_empl_10ya_temp = .
	replace self_empl_10ya_temp = 1 if time==10 & (psoc_status_kode=="110" | psoc_status_kode=="112") 
	replace self_empl_10ya_temp = 0 if time==10 & !mi(psoc_status_kode) & psoc_status_kode!="110" & psoc_status_kode!="112"
	replace self_empl_10ya_temp = 1 if time==10 & (pstill=="11" | pstill=="12" | pstill=="13" | pstill=="14" | pstill=="19")
	replace self_empl_10ya_temp = 0 if time==10 & !mi(pstill) & pstill!="11" & pstill!="12" & pstill!="13" & pstill!="14" & pstill!="19"
	
	gen 	self_empl_10_12ya_temp = .
	replace self_empl_10_12ya_temp = 1 if inrange(time,10,12) & (psoc_status_kode=="110" | psoc_status_kode=="112") 
	replace self_empl_10_12ya_temp = 0 if inrange(time,10,12) & !mi(psoc_status_kode) & psoc_status_kode!="110" & psoc_status_kode!="112"
	replace self_empl_10_12ya_temp = 1 if inrange(time,10,12) & (pstill=="11" | pstill=="12" | pstill=="13" | pstill=="14" | pstill=="19")
	replace self_empl_10_12ya_temp = 0 if inrange(time,10,12) & !mi(pstill) & pstill!="11" & pstill!="12" & pstill!="13" & pstill!="14" & pstill!="19"
	
	by pnr: egen self_empl_10ya    = max(self_empl_10ya_temp)
	by pnr: egen self_empl_10_12ya = max(self_empl_10_12ya_temp)
	drop self_empl_10ya_temp self_empl_10_12ya_temp
	
	* Not in the work force
	destring psoc_status_kode, gen(psoc_status_kode_n)
	destring pstill, gen(pstill_n)

	gen 	out_of_workforce_10ya_temp = .
	replace out_of_workforce_10ya_temp = 1 if time==10 & !mi(psoc_status_kode_n) & psoc_status_kode_n>=300
	replace out_of_workforce_10ya_temp = 0 if time==10 & !mi(psoc_status_kode_n) & psoc_status_kode_n<300
	replace out_of_workforce_10ya_temp = 1 if time==10 & !mi(pstill_n) & pstill_n>=50
	replace out_of_workforce_10ya_temp = 0 if time==10 & !mi(pstill_n) & pstill_n<50
	
	gen 	out_of_workforce_10_12ya_temp = .
	replace out_of_workforce_10_12ya_temp = 1 if inrange(time,10,12) & !mi(psoc_status_kode_n) & psoc_status_kode_n>=300
	replace out_of_workforce_10_12ya_temp = 0 if inrange(time,10,12) & !mi(psoc_status_kode_n) & psoc_status_kode_n<300
	replace out_of_workforce_10_12ya_temp = 1 if inrange(time,10,12) & !mi(pstill_n) & pstill_n>=50
	replace out_of_workforce_10_12ya_temp = 0 if inrange(time,10,12) & !mi(pstill_n) & pstill_n<50
	
	by pnr: egen out_of_workforce_10ya    = max(out_of_workforce_10ya_temp)
	by pnr: egen out_of_workforce_10_12ya = max(out_of_workforce_10_12ya_temp)
	drop out_of_workforce_10ya_temp out_of_workforce_10_12ya_temp
	
	* Unemployment
	replace arledgr = arledgr_brutto if mi(arledgr)
	
	gen months_unemp = (arledgr/1000)*12
	
	cap drop temp
	by pnr: egen temp = sum(months_unemp)  if time>6 & time<10
	by pnr: egen cum_unemp_7_9ya = max(temp)
	label var cum_unemp_7_9ya "Kummuleret antal måneders ledighed 7-9 år efter YOA"
	drop temp
	
	cap drop temp
	by pnr: egen temp = sum(months_unemp)  if time>0 & time<10
	by pnr: egen cum_unemp_1_9ya = max(temp)
	label var cum_unemp_1_9ya "Kummuleret antal måneders ledighed 1-9 år efter YOA"
	drop temp
	
	by pnr: egen temp = sum(months_unemp)  if time>9 & time<13
	by pnr: egen cum_unemp_10_12ya = max(temp)
	label var cum_unemp_10_12ya "Kummuleret antal måneders ledighed 10-12 år efter YOA"
	drop temp
	
	gen 	unemp_10_12ya = 0 if cum_unemp_10_12ya==0
	replace unemp_10_12ya = 1 if cum_unemp_10_12ya>0 & !mi(cum_unemp_10_12ya)
	
	gen 	unemp3m_10_12ya = 0 if cum_unemp_10_12ya<=3
	replace unemp3m_10_12ya = 1 if cum_unemp_10_12ya>3 & !mi(cum_unemp_10_12ya)
	
	gen unemp1_10ya = months_unemp if time==10
	gen unemp2_10ya = (psoc_status_kode_n==200 | pstill_n==40) if (!mi(psoc_status_kode_n) | !mi(pstill_n)) & time==10
	gsort pnr - time
	replace unemp1_10ya= unemp1_10ya[_n-1] if pnr==pnr[_n-1] & mi(unemp1_10ya)
	replace unemp2_10ya= unemp2_10ya[_n-1] if pnr==pnr[_n-1] & mi(unemp2_10ya)
	
	sort pnr time
	
	drop months_unemp

	* Pension savings
	gen temp = pensionsdepot/1000 if time==10
	by pnr: egen pensionsdepot_10ya = max(temp)
	drop temp
	gen temp = pensionsdepot/1000 if year==2016
	by pnr: egen pensionsdepot_2016 = max(temp)
	drop pensionsdepot temp
		
	* Pension saving contributions
	gen temp = qpripen_2015p if time==10
	bysort pnr: egen pens_bidrag_10ya = max(temp)
	gen 	pens_10ya = 0 if pens_bidrag_10ya==0
	replace pens_10ya = 1 if pens_bidrag_10ya>0 & !mi(pens_bidrag_10ya)
	drop temp
	gen arbpens = qarbpenk_2015p+qarbpenl_2015p
	bysort pnr: egen temp=total(arbpens) if inrange(time,0,10)
	bysort pnr: egen arbpens_0_10ya = max(temp)
	cap drop temp
	gen temp = arbpens if time==10
	bysort pnr: egen arbpens_bidrag_10ya = max(temp)
	gen 	arbpens_10ya = 0 if arbpens_bidrag_10ya==0
	replace arbpens_10ya = 1 if arbpens_bidrag_10ya>0 & !mi(arbpens_bidrag_10ya)
	cap drop temp arbpens
		
	* Value of real estate
	gen 	ejendom_10ya_temp = . 
	replace ejendom_10ya_temp = 1 if !mi(ejendomsvurdering) & ejendomsvurdering>0 & time==10
	replace ejendom_10ya_temp = 0 if ejendomsvurdering==0 & time==10
	by pnr: egen ejendom_10ya = max(ejendom_10ya_temp)
	gen ejendomsvurdering_10ya_temp = ejendomsvurdering/1000 if time==10
	by pnr: egen ejendomsvurdering_10ya = max(ejendomsvurdering_10ya_temp)
	drop ejendomsvurdering ejendom_10ya_temp ejendomsvurdering_10ya_temp
	
	* Assets and liabilities
	foreach x in gaeld_til_off_2015p koejd_2015p loenmv_13_2015p oblakt_2015p oblgaeld_2015p pantakt_2015p pantgaeld_2015p {
		gen temp = `x' if time==10
		by pnr: egen `x'_10ya = max(temp)
		drop temp
		gen temp = `x' if year==2016
		by pnr: egen `x'_2016 = max(temp)
		drop temp
	}
	
	* Spouse/partner
	gen gift = (!mi(efalle))
	gen partner_10ya_temp = gift if time==10
	by pnr: egen partner_10ya = max(partner_10ya_temp)
	drop gift partner_10ya_temp
	
	gen gift_temp = (civst=="G") if !mi(civst) & time==10
	by pnr: egen gift = max(gift_temp)
	drop gift_temp
	
	gen audd_n_efalle_10ya = audd_n_efalle if time==10
	gsort pnr - audd_n_efalle_10ya
	replace audd_n_efalle_10ya = audd_n_efalle_10ya[_n-1] if pnr==pnr[_n-1]
	sort pnr year
	
	gen disced_audd_efalle_10ya = disced_audd_efalle if time==10
	gsort pnr - disced_audd_efalle_10ya
	replace disced_audd_efalle_10ya = disced_audd_efalle_10ya[_n-1] if pnr==pnr[_n-1]
	sort pnr year
	
	gen audd_niveau_efalle_10ya = audd_niveau_efalle if time==10
	gsort pnr - audd_niveau_efalle_10ya
	replace audd_niveau_efalle_10ya = audd_niveau_efalle_10ya[_n-1] if pnr==pnr[_n-1]
	sort pnr year
	
	gen erhvervsindk_efalle_10ya_temp = erhvervsindk_efalle if time==10
	by pnr: egen erhvervsindk_efalle_10ya = max(erhvervsindk_efalle_10ya_temp)
	drop erhvervsindk_efalle_10ya_temp
	replace erhvervsindk_efalle_10ya = erhvervsindk_efalle_10ya/1000
	
	forval x=9/11 {
		gen perindkialt_efalle_`x'ya_temp = perindkialt_efalle if time==`x'
		by pnr: egen perindkialt_efalle_`x'ya = max(perindkialt_efalle_`x'ya_temp)
		drop perindkialt_efalle_`x'ya_temp
		replace perindkialt_efalle_`x'ya = perindkialt_efalle_`x'ya/1000
		
		egen household_inc_`x'ya = rowtotal(perindkialt_efalle_`x'ya persindk_`x'ya), missing
	}
	egen household_inc_9_11ya = rowmean(household_inc_9ya household_inc_10ya household_inc_11ya)
	
	foreach x in efalle 35_efalle 40_efalle 45_efalle {
		forval y=10/12 {
			gen inc_rank_temp = inc_rank_`x' if time==`y'
			by pnr: egen inc_rank_`x'_`y'ya = max(inc_rank_temp)
			drop inc_rank_temp
		}
	}
	
	
	* Age at application
	gen alder_yoa_temp=age if time==0
	by pnr: egen alder_yoa=max(alder_yoa_temp)
	drop alder_yoa_temp
	label var alder_yoa "Alder YOA"

	gen alder_yoa_1=(alder_yoa<20)
	gen alder_yoa_2=(alder_yoa==20 | alder_yoa==21)
	gen alder_yoa_3=(alder_yoa==22 | alder_yoa==23)
	gen alder_yoa_4=(alder_yoa==24 | alder_yoa==25)
	gen alder_yoa_5=(alder_yoa>25)

	forval x=1/5 {
		replace alder_yoa_`x'=. if mi(alder_yoa)
	}

	label var alder_yoa_1 "Age < 20"
	label var alder_yoa_2 "Age 20-21"
	label var alder_yoa_3 "Age 22-23"
	label var alder_yoa_4 "Age 24-25"
	label var alder_yoa_5 "Age > 25"

	gen 	aldersgrp=.
	replace aldersgrp=1 if alder_yoa_1==1
	replace aldersgrp=2 if alder_yoa_2==1
	replace aldersgrp=3 if alder_yoa_3==1
	replace aldersgrp=4 if alder_yoa_4==1
	replace aldersgrp=5 if alder_yoa_5==1
	label var aldersgrp "Age Group"


	* Sector 
	gen 	priv_sec = .
	replace priv_sec = 1 if sektor == "1"
	replace priv_sec = 0 if sektor == "2" |  sektor == "3"
	replace priv_sec = 1 if arb_sektorkode == 11 // Statslige ikke-finansilelle...
	replace priv_sec = 1 if arb_sektorkode == 12 // Kommunale ikke-finansielle...
	replace priv_sec = 1 if arb_sektorkode == 14 // Statsligt ejede ikke-finansielle...
	replace priv_sec = 1 if arb_sektorkode == 15 // Kommunalt ejede ikke-finansielle...
	replace priv_sec = 1 if arb_sektorkode == 16 // Regionsejede ikke finansielle...
	replace priv_sec = 1 if arb_sektorkode == 18 // Private ikke-finansielle...
	replace priv_sec = 1 if arb_sektorkode == 21 // Centralbanken
	replace priv_sec = 1 if arb_sektorkode == 27 // Offentlige pengeinstitutter...
	replace priv_sec = 1 if arb_sektorkode == 28 // Private pengeinstitutter...
	replace priv_sec = 1 if arb_sektorkode == 38 // Private investeringsforeninger...
	replace priv_sec = 1 if arb_sektorkode == 41 // Andre offentlige finansielle...
	replace priv_sec = 1 if arb_sektorkode == 42 // Andre private finansielle..
	replace priv_sec = 1 if arb_sektorkode == 48 // Private finansielle...
	replace priv_sec = 1 if arb_sektorkode == 51 // Offentlige koncerntilknyttede...
	replace priv_sec = 1 if arb_sektorkode == 52 // Private koncerntilknyttede...
	replace priv_sec = 1 if arb_sektorkode == 57 // Offentlige forsikringsselskaber
	replace priv_sec = 1 if arb_sektorkode == 58 // Private forsikringsselskaber
	replace priv_sec = 1 if arb_sektorkode == 62 // Private pensionskasser
	replace priv_sec = 0 if arb_sektorkode == 71 // Statslig forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 72 // Statslig forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 74 // Regionernes forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 75 // Regionernes forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 76 // Kommunal forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 77 // Kommunal forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 79 // Sociale kasser og fonde
	replace priv_sec = 1 if arb_sektorkode == 81 // Personligt ejede virksomheder
	*replace priv_sec =  if arb_sektorkode == 89 // Non-profit...
	*replace priv_sec =  if arb_sektorkode == 91 // Rest of the world...
	*replace priv_sec =  if arb_sektorkode == 99 // Ikke oplyst
	
	replace priv_sec=. if time!=10
	replace priv_sec=priv_sec[_n-1] if pnr==pnr[_n-1] & mi(priv_sec)
	gsort pnr - year
	replace priv_sec=priv_sec[_n-1] if pnr==pnr[_n-1] & mi(priv_sec)
	sort pnr year
	
	* Industry - Based on DB03 (level 4) for persbrc and DB07 (level 3) for arb_hoved_bra_db07
	destring persbrc, gen(persbrc_n)
	gen 	bra_undervisning = 0 if !mi(arb_hoved_bra_db07) | !mi(persbrc)
	replace bra_undervisning = 1 if inrange(persbrc_n         ,800000,809999)
	replace bra_undervisning = 1 if inrange(arb_hoved_bra_db07,850000,859999)
	gen 	bra_sundhed      = 0 if !mi(arb_hoved_bra_db07) | !mi(persbrc)
	replace bra_sundhed      = 1 if inrange(persbrc_n         ,850000,859999)
	replace bra_sundhed      = 1 if inrange(arb_hoved_bra_db07,860000,869999)
	gen 	bra_offadm       = 0 if !mi(arb_hoved_bra_db07) | !mi(persbrc)
	replace bra_offadm       = 1 if inrange(persbrc_n         ,750000,759999)
	replace bra_offadm       = 1 if inrange(arb_hoved_bra_db07,840000,849999)
	gen 	bra_finans       = 0 if !mi(arb_hoved_bra_db07) | !mi(persbrc)
	replace bra_finans       = 1 if inrange(persbrc_n         ,650000,679999)
	replace bra_finans       = 1 if inrange(arb_hoved_bra_db07,640000,669999)
	
	foreach x in undervisning sundhed offadm finans {
		gen 	bra_`x'_10ya = bra_`x' if time==10
		replace bra_`x'_10ya=bra_`x'_10ya[_n-1] if pnr==pnr[_n-1]  & mi(bra_`x'_10ya)
	}
	gsort pnr - year 
	foreach x in undervisning sundhed offadm finans {
		replace bra_`x'_10ya=bra_`x'_10ya[_n-1] if pnr==pnr[_n-1] & mi(bra_`x'_10ya)
	}
	sort pnr year
	drop bra_undervisning bra_sundhed bra_offadm bra_finans  
	
	* Managerial work
	destring disco_kode, gen(disco_n)
	gen manager_temp = (inrange(disco_n,100000,199999)) if !mi(disco_kode) & time==11
	by pnr: egen manager = max(manager_temp)
	drop manager_temp disco_n
	
	* Public debt
	gen temp=(gaeld_til_off>0) if time>9 & !mi(gaeld_til_off)
	bysort pnr: egen pub_deb_10_all = max(temp)
	drop temp
	gen temp=(gaeld_til_off>0) if time==10 & !mi(gaeld_til_off)
	bysort pnr: egen pub_deb_10ya = max(temp)
	drop temp
	gen temp=(gaeld_til_off>0) if year==2016 & !mi(gaeld_til_off)
	bysort pnr: egen pub_deb_2016 = max(temp)
	drop temp

	* Variables related to parents
	foreach x in audd_niveau_far inc_rank_35_far inc_rank_40_far inc_rank_45_far labor_inc_4044_far total_inc_4044_far audd_niveau_mor inc_rank_35_mor inc_rank_40_mor inc_rank_45_mor labor_inc_4044_mor total_inc_4044_mor {
		replace `x'= `x'[_n-1] if mi(`x') & pnr==pnr[_n-1]
	}
	
	* Student job
	tab psoc_status_kode if inrange(time,1,6) & !mi(udd)
		* 134 Lønmodtager grundniveau
		* 511 Under uddannelse
		* 132 Lønmodtager højeste niveau
		* 133 Lønmodtager mellemniveau
		* 135 Andre lønmodtagere
	tab pstill if inrange(time,1,6) & !mi(udd) & year<1996
		* 91  Under uddannelse
		* 37  Andre lønmodtagere
		* 36  Ikke faglært arbejder
	tab pstill if inrange(time,1,6) & !mi(udd) & year>=1996
		* 35  Lønmodtager 
		* 91  Under uddannelse
		* 37  Andre lønmodtagere
		* 34  Lønmodtager mellemniveau
		* 32  Lønmodtager højeste niveau
		* 36  Andre lønmodtagere
		
	cap drop study_emp_high 
	cap drop temp
	gen		temp = 0 if inrange(time,1,6) & !mi(udd)
	replace temp = 1 if inrange(time,1,6) & !mi(udd) & psoc_status_kode_n==132
	replace temp = 1 if inrange(time,1,6) & !mi(udd) & pstill_n==32
	by pnr: egen study_emp_high = max(temp)

	* DB93 I
		* Finance and insurance 65-67
		* Undervisning videregående uddannelse 803
		* RD 73
	* DB93 II 
		* Finance and insurance 65-67
		* Undervisning videregående uddannelse 803
		* RD 73
	* DB03
		* Finance and insurance 65-67
		* Undervisning videregående uddannelse 803
		* RD 73
	* DB07 
		* Finance and insurance 64-66
		* RD 72
		* Undervisning videregående uddannelse 854
		
	cap drop temp 
	cap drop study_emp_finance
	gen		temp = 0 if inrange(time,1,6) & !mi(udd)
	replace temp = 1 if inrange(time,1,6) & !mi(udd) & inrange(persbrc_n,650000,679999) 
	replace temp = 1 if inrange(time,1,6) & !mi(udd) & inrange(arb_hoved_bra_db07,640000,669999)
	by pnr: egen study_emp_finance = max(temp)

	cap drop temp 
	cap drop study_emp_edu
	gen		temp = 0 if inrange(time,1,6) & !mi(udd)
	replace temp = 1 if inrange(time,1,6) & !mi(udd) & inrange(persbrc_n,803000,803999) 
	replace temp = 1 if inrange(time,1,6) & !mi(udd) & inrange(arb_hoved_bra_db07,854000,854999)
	by pnr: egen study_emp_edu = max(temp)

	cap drop temp 
	cap drop rd
	gen		temp = 0 if inrange(time,1,6) & !mi(udd)
	replace temp = 1 if inrange(time,1,6) & !mi(udd) & inrange(persbrc_n,730000,739999) 
	replace temp = 1 if inrange(time,1,6) & !mi(udd) & inrange(arb_hoved_bra_db07,720000,729999)
	by pnr: egen study_emp_rd = max(temp)
	drop temp
	
	foreach x in study_emp_high study_emp_finance study_emp_edu study_emp_rd {
		replace `x'=0 if mi(`x')
	}

	
	* Keep relevant variables and collapse to one observation per applicant
	drop year age udd audd alder civ_vfra civst efalle far_id foed_dag ie_type kom mor_id befupdsourceyear arblhu arblhumv atpsaml bankakt bankgaeld dispon_13 erhvervsindk_13 folkefortid_13 fosfufrd gaeld_til_off honny indestpi jubjub kapindkp kapitialt kapitpriv kapitvirk koejd kontanthj_13 kursakt loenmv_13 netovskud_13 oblakt oblgaeld off_overforsel_13 offpens offpens_efterlon_13 pantakt pantgaeld perindkialt_13 peroevrigformue_13 qaktie qaktivf qaktivf_ny05 qarblos qbistyd qpassiv qpassivn indupd01sourceyear qarbpenk qarbpenl qpripen qpripenk qpripenl indhsourceyear arledgr arledgr_brutto arledgr_netto erhver psoc_status_kode psoc_status_kode_n pstill pstill_n idapupdsourceyear ansdage_2008 arb_hoved_bra_db07 arb_sektorkode disco_kode disco_matchprio_kode jobkat persbrc persbrc_n persbrc07 sektor idanupdsourceyear max_ans rest_all rest41 rest42 rest44 rest47 rest52 rest53 rest54 rest55 rest56 rest57 rest58 rest59 rest60 rest64 rest66 max_rente antal_restance hfinstnr ig_vfra uddaupd01sourceyear karakter_udd skala udgsourceyear slutaar birthyear cpi bankakt_2015p bankgaeld_2015p dispon_13_2015p erhvervsindk_13_2015p gaeld_til_off_2015p kursakt_2015p oblakt_2015p oblgaeld_2015p pantakt_2015p pantgaeld_2015p perindkialt_13_2015p fosfufrd_2015p qarbpenk_2015p qarbpenl_2015p qpripen_2015p qpripenk_2015p qpripenl_2015p audd_efalle erhvervsindk_13_2015p_efalle perindkialt_13_2015p_efalle disced_audd_efalle audd_niveau_efalle audd_n_efalle erhvervsindk_efalle perindkialt_efalle audd_far labor_inc_4044_far total_inc_4044_far audd_mor labor_inc_4044_mor total_inc_4044_mor udd_navn udd_niveau udd_field2 udd_field3 udd_field4 audd_navn audd_niveau audd_field2 audd_field3 audd_field4   antboernf audd_stem audd_oeko audd_jura audd_huaa audd_educ audd_welf audd_medi audd_sosc audd_econ udd_stem udd_oeko udd_jura udd_huaa udd_educ udd_welf udd_medi udd_sosc udd_econ blb_rest41 blb_rest42 blb_rest44 blb_rest47 blb_rest52 blb_rest53 blb_rest54 blb_rest55 blb_rest56 blb_rest57 blb_rest58 blb_rest59 blb_rest60 blb_rest64 blb_rest66 rest_all_efalle rest41_efalle rest42_efalle rest44_efalle rest47_efalle rest52_efalle rest53_efalle rest54_efalle rest55_efalle rest56_efalle rest57_efalle rest58_efalle rest59_efalle rest60_efalle rest64_efalle rest66_efalle rest67_efalle max_rente_efalle antal_restance_efalle blb_rest41_efalle blb_rest42_efalle blb_rest44_efalle blb_rest47_efalle blb_rest52_efalle blb_rest53_efalle blb_rest54_efalle blb_rest55_efalle blb_rest56_efalle blb_rest57_efalle blb_rest58_efalle blb_rest59_efalle blb_rest60_efalle blb_rest64_efalle blb_rest66_efalle blb_rest67_efalle
	
	keep if time==0
	drop time
	
	gen t=1 if pnr!=pnr[_n-1] 
	count if !mi(t) /* 53,882 */
	drop t
	
	* Year of Application Dummies
	tab yoa, gen(yoa)
	label var yoa1  "YOA=1993"
	label var yoa2  "YOA=1994"
	label var yoa3  "YOA=1995"
	label var yoa4  "YOA=1996"
	label var yoa5  "YOA=1997"
	label var yoa6  "YOA=1998"
	label var yoa7  "YOA=1999"
	label var yoa8  "YOA=2000"
	label var yoa9  "YOA=2001"
	label var yoa10 "YOA=2002"
	label var yoa11 "YOA=2003"
	label var yoa12 "YOA=2004"
	label var yoa13 "YOA=2005"
	label var yoa14 "YOA=2006"

	* Label 
	label var sex "Sex (=1 if male)"

	* Save
	compress
	save "$temp\analyse_del_2"
	
********************************************************************************

	* Education of spouse
	gen efal_edu_1 = (audd_n_efalle_10ya==1) if !mi(audd_n_efalle_10ya)
	gen efal_edu_2 = (audd_n_efalle_10ya==2) if !mi(audd_n_efalle_10ya)
	gen efal_edu_3 = (audd_n_efalle_10ya==3) if !mi(audd_n_efalle_10ya)
	gen efal_edu_4 = (audd_n_efalle_10ya==4) if !mi(audd_n_efalle_10ya)
	gen efal_edu_5 = (audd_n_efalle_10ya==5) if !mi(audd_n_efalle_10ya)
	gen efal_edu_6 = (audd_n_efalle_10ya==6) if !mi(audd_n_efalle_10ya)
	gen efal_edu_7 = (audd_n_efalle_10ya==7) if !mi(audd_n_efalle_10ya)
	gen efal_edu_8 = (audd_n_efalle_10ya==8) if !mi(audd_n_efalle_10ya)

	gen 	efal_mas = 0 if !mi(audd_niveau_efalle_10ya) 
	replace efal_mas = 1 if audd_niveau_efalle_10ya=="Lange videregående uddannelser"
	replace efal_mas = 1 if audd_niveau_efalle_10ya=="Ph.d. og forskeruddannelser"

	* Multiply distance to threshold with 10
	gen dist10 = dist*10
	
	* Interaction term
	gen over_X_dist = over*dist

	* DISCED field groups
	tab audd_field4_10ya, gen(field_temp)

	gen 	disced_audd_n = . 
	replace disced_audd_n = 1  if field_temp1  == 1
	replace disced_audd_n = 2  if field_temp2  == 1 
	replace disced_audd_n = 3  if field_temp3  == 1
	replace disced_audd_n = 4  if field_temp4  == 1
	replace disced_audd_n = 5  if field_temp5  == 1
	replace disced_audd_n = 6  if field_temp8  == 1
	replace disced_audd_n = 7  if field_temp9  == 1
	replace disced_audd_n = 8  if field_temp10 == 1
	replace disced_audd_n = 9  if field_temp11 == 1
	replace disced_audd_n = 10 if field_temp12 == 1

	drop field_temp*

	gen 	kot1_disced = .
	replace kot1_disced = 1  if kot1_field_l4=="Agriculture, forestry, fisheries  and veterinary"
	replace kot1_disced = 2  if kot1_field_l4=="Arts and humanities"
	replace kot1_disced = 3  if kot1_field_l4=="Business, administration and law "
	replace kot1_disced = 4  if kot1_field_l4=="Education"
	replace kot1_disced = 5  if kot1_field_l4=="Engineering, manufacturing and construction"
	replace kot1_disced = 6  if kot1_field_l4=="Health and welfare"
	replace kot1_disced = 7  if kot1_field_l4=="Information and communication technologies (ICTs)"
	replace kot1_disced = 8  if kot1_field_l4=="Natural sciences, mathematics and statistics"
	replace kot1_disced = 9  if kot1_field_l4=="Services"
	replace kot1_disced = 10 if kot1_field_l4=="Social sciences, journalism and information" 

	gen 	kot2_disced = .
	replace kot2_disced = 1  if kot2_field_l4=="Agriculture, forestry, fisheries  and veterinary"
	replace kot2_disced = 2  if kot2_field_l4=="Arts and humanities"
	replace kot2_disced = 3  if kot2_field_l4=="Business, administration and law "
	replace kot2_disced = 4  if kot2_field_l4=="Education"
	replace kot2_disced = 5  if kot2_field_l4=="Engineering, manufacturing and construction"
	replace kot2_disced = 6  if kot2_field_l4=="Health and welfare"
	replace kot2_disced = 7  if kot2_field_l4=="Information and communication technologies (ICTs)"
	replace kot2_disced = 8  if kot2_field_l4=="Natural sciences, mathematics and statistics"
	replace kot2_disced = 9  if kot2_field_l4=="Services"
	replace kot2_disced = 10 if kot2_field_l4=="Social sciences, journalism and information"      

	gen 	comp1_disced = 0
	replace	comp1_disced = 1 if kot1_disced == disced_audd_n

	gen 	comp2_disced = 0
	replace	comp2_disced = 1 if kot2_disced == disced_audd_n

	* Fields of study used in the paper
	gen 	audd_n = . 
	replace audd_n = 1  if stem_10ya == 1
	replace audd_n = 2  if oeko_10ya == 1 
	replace audd_n = 3  if jura_10ya == 1
	replace audd_n = 4  if huaa_10ya == 1
	replace audd_n = 5  if educ_10ya == 1
	replace audd_n = 6  if welf_10ya == 1
	replace audd_n = 7  if medi_10ya == 1
	replace audd_n = 8  if sosc_10ya == 1

	gen 	kot1_field = .
	replace kot1_field = 1  if kot1_stem==1
	replace kot1_field = 2  if kot1_oeko==1
	replace kot1_field = 3  if kot1_jura==1
	replace kot1_field = 4  if kot1_huaa==1
	replace kot1_field = 5  if kot1_educ==1
	replace kot1_field = 6  if kot1_welf==1
	replace kot1_field = 7  if kot1_medi==1
	replace kot1_field = 8  if kot1_sosc==1

	gen 	kot2_field = .
	replace kot2_field = 1  if kot2_stem==1
	replace kot2_field = 2  if kot2_oeko==1
	replace kot2_field = 3  if kot2_jura==1
	replace kot2_field = 4  if kot2_huaa==1
	replace kot2_field = 5  if kot2_educ==1
	replace kot2_field = 6  if kot2_welf==1
	replace kot2_field = 7  if kot2_medi==1
	replace kot2_field = 8  if kot2_sosc==1

	tab kot1_field_l4 if mi(kot1_field)  
	tab kot1_field_l3 if mi(kot1_field) 
	tab kot1_field_l2 if mi(kot1_field) 
	replace kot1_field = 4 if mi(kot1_field) & kot1_field_l3=="Languages"
	replace kot1_field = 8 if mi(kot1_field) & kot1_field_l2=="Sociology and cultural studies"
	replace kot1_field = 1 if mi(kot1_field) & kot1_field_l2=="Statistics"
	replace kot1_field = 6 if mi(kot1_field) & kot1_field_l2=="Therapy and rehabilitation"

	tab kot2_field_l4 if mi(kot2_field)  
	tab kot2_field_l3 if mi(kot2_field) 
	tab kot2_field_l2 if mi(kot2_field) 
	replace kot2_field = 4 if mi(kot2_field) & kot2_field_l3=="Languages"
	replace kot2_field = 1 if mi(kot2_field) & kot2_field_l3=="Engineering and engineering trades"
	replace kot2_field = 1 if mi(kot2_field) & kot2_field_l4=="Natural sciences, mathematics and statistics"
	replace kot2_field = 1 if mi(kot2_field) & kot2_field_l2=="Therapy and rehabilitation"


	* Completion indicators
	gen 	comp1 = 0
	replace	comp1 = 1 if kot1_field == audd_n

	gen 	comp2 = 0
	replace	comp2 = 1 if kot2_field == audd_n

	foreach x in stem oeko jura huaa educ welf medi sosc {
		egen 	`x'_sum = rowtotal(`x'_5ya `x'_6ya `x'_7ya `x'_8ya `x'_9ya `x'_10ya)
		gen 	`x'_dum=0 if !mi(`x'_sum)
		replace `x'_dum=1 if `x'_sum>0
	}
	egen completed_fields = rowtotal(stem_dum oeko_dum jura_dum huaa_dum educ_dum welf_dum medi_dum sosc_dum)
	tab completed_fields
		
	* Generate variables for joint estimation
	gen f1 = kot1_field
	gen f2 = kot2_field
	gen c  = audd_n
	gen z  = over

	forval x=1/8 {
		forval y=1/8 {
			if `x'!=`y' {
				gen cf2_`x'_`y' = (c==`x' & f2==`y') if !mi(c)
				gen zf2_`x'_`y' = (z==1 & f1==`x' & f2==`y')
				if `x' == 1 {
					label var cf2_`x'_`y' "\hspace{20pt}STEM"
				}
				if `x' == 2 {
					label var cf2_`x'_`y' "\hspace{20pt}Economics"
				}
				if `x' == 3 {
					label var cf2_`x'_`y' "\hspace{20pt}Law"
				}
				if `x' == 4 {
					label var cf2_`x'_`y' "\hspace{20pt}Hum., Art \& Arch."
				}
				if `x' == 5 {
					label var cf2_`x'_`y' "\hspace{20pt}Education"
				}
				if `x' == 6 {
					label var cf2_`x'_`y' "\hspace{20pt}Welfare"
				}
				if `x' == 7 {
					label var cf2_`x'_`y' "\hspace{20pt}Medicine"
				}
				if `x' == 8 {
					label var cf2_`x'_`y' "\hspace{20pt}Soc. Science"
				}
			}
		}
	}

	* Age groups
	drop if alder_yoa<18 // 13
	cap drop aldersgrp
	gen 	aldersgrp = .
	replace aldersgrp = 1 if alder_yoa==18 | alder_yoa==19
	replace aldersgrp = 2 if alder_yoa==20
	replace aldersgrp = 3 if alder_yoa==21
	replace aldersgrp = 4 if alder_yoa==22
	replace aldersgrp = 5 if alder_yoa==23 | alder_yoa==24
	replace aldersgrp = 6 if alder_yoa>24 & !mi(alder_yoa)

	* Default and delinquency indicator
	gen 	restance = rest8_10_all
	sum restance
	replace restance = 0 if mi(restance) & in_irte_10_all==1
	sum restance

	* Income dummies
	cap drop inc_dum
	gen 	inc_dum = .
	replace inc_dum = 1 if inrange(inc_rank_10ya, 1,20)
	replace inc_dum = 2 if inrange(inc_rank_10ya,21,40)
	replace inc_dum = 3 if inrange(inc_rank_10ya,41,60)
	replace inc_dum = 4 if inrange(inc_rank_10ya,61,80)
	replace inc_dum = 5 if inrange(inc_rank_10ya,81,100)
	replace inc_dum = 1 if inrange(inc_rank_9ya, 1,20)   & mi(inc_dum)
	replace inc_dum = 2 if inrange(inc_rank_9ya,21,40)   & mi(inc_dum)
	replace inc_dum = 3 if inrange(inc_rank_9ya,41,60)   & mi(inc_dum)
	replace inc_dum = 4 if inrange(inc_rank_9ya,61,80)   & mi(inc_dum)
	replace inc_dum = 5 if inrange(inc_rank_11ya,81,100) & mi(inc_dum)
	replace inc_dum = 1 if inrange(inc_rank_11ya, 1,20)  & mi(inc_dum)
	replace inc_dum = 2 if inrange(inc_rank_11ya,21,40)  & mi(inc_dum)
	replace inc_dum = 3 if inrange(inc_rank_11ya,41,60)  & mi(inc_dum)
	replace inc_dum = 4 if inrange(inc_rank_11ya,61,80)  & mi(inc_dum)
	replace inc_dum = 5 if inrange(inc_rank_11ya,81,100) & mi(inc_dum)

	* Apply age criterion for admission when possible
	sort yoa kotnr1 pnr

	local if "if dist==0 & optkv1st_1==0 & optkv2_1==0 & optkv2st_1==0" 
	replace over=0 `if' & yoa==1997 & kotnr1==11550 & age_application>ym(1975,04)
	replace over=0 `if' & yoa==1998 & kotnr1==11560 & age_application>ym(1974,12)
	replace over=0 `if' & yoa==1998 & kotnr1==13300 & age_application>ym(1975,12)
	replace over=0 `if' & yoa==1999 & kotnr1==11120 & age_application>ym(1976,01)
	replace over=0 `if' & yoa==1999 & kotnr1==13300 & age_application>ym(1976,11)
	replace over=0 `if' & yoa==1999 & kotnr1==28010 & age_application>ym(1979,03)
	replace over=0 `if' & yoa==2000 & kotnr1==13300 & age_application>ym(1979,10)
	replace over=0 `if' & yoa==2002 & kotnr1==21276 & age_application>ym(1980,01)
	replace over=0 `if' & yoa==2004 & kotnr1==22415 & age_application>ym(1983,04)
	replace over=0 `if' & yoa==2004 & kotnr1==25435 & age_application>ym(1979,08)
	replace over=0 `if' & yoa==2004 & kotnr1==30060 & age_application>ym(1983,01)
	replace over=0 `if' & yoa==2005 & kotnr1==17410 & age_application>ym(1975,10)
	replace over=0 `if' & yoa==2005 & kotnr1==22010 & age_application>ym(1985,02)
	replace over=0 `if' & yoa==2006 & kotnr1==22415 & age_application>ym(1979,05)

	local if "if dist==0 & optkv1st_1==0 & optkv2_1==0 & optkv2st_1==0" 
	replace over=0 `if' & yoa==1993 & kotnr1==13200 & age_application>ym(1965,12)
	replace over=0 `if' & yoa==1993 & kotnr1==28060 & age_application>ym(1965,11)
	replace over=0 `if' & yoa==1993 & kotnr1==31501 & age_application>ym(1973,04)
	replace over=0 `if' & yoa==1994 & kotnr1==11420 & age_application>ym(1971,07)
	replace over=0 `if' & yoa==1994 & kotnr1==28010 & age_application>ym(1974,11)
	replace over=0 `if' & yoa==1994 & kotnr1==34801 & age_application>ym(1969,11)
	replace over=0 `if' & yoa==1994 & kotnr1==41520 & age_application>ym(1964,07)
	replace over=0 `if' & yoa==1995 & kotnr1==11400 & age_application>ym(1973,08)
	replace over=0 `if' & yoa==1995 & kotnr1==11420 & age_application>ym(1973,09)
	replace over=0 `if' & yoa==1995 & kotnr1==12140 & age_application>ym(1970,12)
	replace over=0 `if' & yoa==1995 & kotnr1==21250 & age_application>ym(1973,09)
	replace over=0 `if' & yoa==1995 & kotnr1==31501 & age_application>ym(1975,02)
	replace over=0 `if' & yoa==1996 & kotnr1==11420 & age_application>ym(1970,11)
	replace over=0 `if' & yoa==1996 & kotnr1==14220 & age_application>ym(1974,06)
	replace over=0 `if' & yoa==1996 & kotnr1==28060 & age_application>ym(1976,12)
	replace over=0 `if' & yoa==1997 & kotnr1==13200 & age_application>ym(1975,06)
	replace over=0 `if' & yoa==1997 & kotnr1==14220 & age_application>ym(1976,09)
	replace over=0 `if' & yoa==1998 & kotnr1==11420 & age_application>ym(1976,10)
	replace over=0 `if' & yoa==1998 & kotnr1==11580 & age_application>ym(1976,03)
	replace over=0 `if' & yoa==1998 & kotnr1==13200 & age_application>ym(1977,12)
	replace over=0 `if' & yoa==1998 & kotnr1==28010 & age_application>ym(1977,07)
	replace over=0 `if' & yoa==1998 & kotnr1==31001 & age_application>ym(1975,01)
	replace over=0 `if' & yoa==1998 & kotnr1==31501 & age_application>ym(1975,09)
	replace over=0 `if' & yoa==1998 & kotnr1==32001 & age_application>ym(1970,12)
	replace over=0 `if' & yoa==1998 & kotnr1==32501 & age_application>ym(1975,01)
	replace over=0 `if' & yoa==1998 & kotnr1==41720 & age_application>ym(1975,09)
	replace over=0 `if' & yoa==1999 & kotnr1==11560 & age_application>ym(1975,01)
	replace over=0 `if' & yoa==1999 & kotnr1==12110 & age_application>ym(1976,12)
	replace over=0 `if' & yoa==1999 & kotnr1==12140 & age_application>ym(1972,05)
	replace over=0 `if' & yoa==1999 & kotnr1==14240 & age_application>ym(1973,05)
	replace over=0 `if' & yoa==1999 & kotnr1==14390 & age_application>ym(1977,01)
	replace over=0 `if' & yoa==1999 & kotnr1==21110 & age_application>ym(1977,12)
	replace over=0 `if' & yoa==2000 & kotnr1==11412 & age_application>ym(1975,01)
	replace over=0 `if' & yoa==2000 & kotnr1==11468 & age_application>ym(1978,07)
	replace over=0 `if' & yoa==2000 & kotnr1==11580 & age_application>ym(1977,07)
	replace over=0 `if' & yoa==2000 & kotnr1==14090 & age_application>ym(1980,09)
	replace over=0 `if' & yoa==2000 & kotnr1==21150 & age_application>ym(1979,04)
	replace over=0 `if' & yoa==2000 & kotnr1==21180 & age_application>ym(1977,02)
	replace over=0 `if' & yoa==2000 & kotnr1==28010 & age_application>ym(1978,06)
	replace over=0 `if' & yoa==2000 & kotnr1==31001 & age_application>ym(1978,02)
	replace over=0 `if' & yoa==2000 & kotnr1==31501 & age_application>ym(1970,12)
	replace over=0 `if' & yoa==2000 & kotnr1==41820 & age_application>ym(1979,01)
	replace over=0 `if' & yoa==2001 & kotnr1==11550 & age_application>ym(1980,06)
	replace over=0 `if' & yoa==2001 & kotnr1==14240 & age_application>ym(1979,04)
	replace over=0 `if' & yoa==2001 & kotnr1==28031 & age_application>ym(1979,05)
	replace over=0 `if' & yoa==2001 & kotnr1==33010 & age_application>ym(1980,08)
	replace over=0 `if' & yoa==2001 & kotnr1==41620 & age_application>ym(1979,05)
	replace over=0 `if' & yoa==2002 & kotnr1==11460 & age_application>ym(1974,11)
	replace over=0 `if' & yoa==2002 & kotnr1==12120 & age_application>ym(1981,02)
	replace over=0 `if' & yoa==2002 & kotnr1==13200 & age_application>ym(1973,02)
	replace over=0 `if' & yoa==2002 & kotnr1==14240 & age_application>ym(1979,02)
	replace over=0 `if' & yoa==2002 & kotnr1==57801 & age_application>ym(1976,01)
	replace over=0 `if' & yoa==2003 & kotnr1==11110 & age_application>ym(1983,01)
	replace over=0 `if' & yoa==2003 & kotnr1==11412 & age_application>ym(1980,02)
	replace over=0 `if' & yoa==2003 & kotnr1==12140 & age_application>ym(1981,05)
	replace over=0 `if' & yoa==2003 & kotnr1==24118 & age_application>ym(1981,11)
	replace over=0 `if' & yoa==2003 & kotnr1==31501 & age_application>ym(1979,10)
	replace over=0 `if' & yoa==2003 & kotnr1==33010 & age_application>ym(1981,07)
	replace over=0 `if' & yoa==2003 & kotnr1==56201 & age_application>ym(1983,11)
	replace over=0 `if' & yoa==2004 & kotnr1==13010 & age_application>ym(1982,12)
	replace over=0 `if' & yoa==2004 & kotnr1==17115 & age_application>ym(1973,04)
	replace over=0 `if' & yoa==2004 & kotnr1==17415 & age_application>ym(1977,04)
	replace over=0 `if' & yoa==2004 & kotnr1==22010 & age_application>ym(1982,08)
	replace over=0 `if' & yoa==2004 & kotnr1==22225 & age_application>ym(1980,10)
	replace over=0 `if' & yoa==2004 & kotnr1==22420 & age_application>ym(1983,03)
	replace over=0 `if' & yoa==2004 & kotnr1==22425 & age_application>ym(1984,06)
	replace over=0 `if' & yoa==2005 & kotnr1==16030 & age_application>ym(1982,09)
	replace over=0 `if' & yoa==2005 & kotnr1==22420 & age_application>ym(1980,08)
	replace over=0 `if' & yoa==2005 & kotnr1==46150 & age_application>ym(1982,06)
	replace over=0 `if' & yoa==2005 & kotnr1==46160 & age_application>ym(1982,12)
	replace over=0 `if' & yoa==2006 & kotnr1==15050 & age_application>ym(1984,06)
	replace over=0 `if' & yoa==2006 & kotnr1==22010 & age_application>ym(1986,02)
	replace over=0 `if' & yoa==2006 & kotnr1==47140 & age_application>ym(1985,04)
	replace over=0 `if' & yoa==2006 & kotnr1==50010 & age_application>ym(1978,08)
	
	* Winzorize
	foreach x in erhvervsindk_1_9ya erhvervsindk_9_11ya persindk_1_9ya persindk_9_11ya household_inc_10ya household_inc_9_11ya {
		qui sum `x', d
		replace `x' = r(p1)  if `x'<r(p1)  & !mi(`x')
		replace `x' = r(p99) if `x'>r(p99) & !mi(`x')
	}
	
	* Educational names (note these are subject to change by Statistics Denmark)
	rename audd_0ya start
	sort start
	merge m:1 start using "\\srvfsenas1\data\Formater\SAS formater i Danmarks Statistik\STATA_datasaet\Disced\c_audd_audd_l1l1_t"
	drop if _merge==2
	drop _merge
	merge m:1 start using "\\srvfsenas1\data\Formater\SAS formater i Danmarks Statistik\STATA_datasaet\Disced\c_audd_fag_l1l2_t"
	drop if _merge==2
	drop _merge
	merge m:1 start using "\\srvfsenas1\data\Formater\SAS formater i Danmarks Statistik\STATA_datasaet\Disced\c_audd_hoved_l1l2_t"
	drop if _merge==2
	drop _merge
	rename start audd_0ya
	rename AUDD_AUDD_L1L1_T audd_0ya_navn
	rename AUDD_FAG_L1L2_T audd_0ya_fag
	rename AUDD_HOVED_L1L2_T audd_0ya_hoved
	
	* Generate indicator for default excl. years of the financial crisis
	forval x=2003/2018 {
		foreach y in 41 44 47 53 54 55 56 57 58 59 60 {
			replace rest`y'_`x'=0 if mi(rest`y'_`x')
		}
	}
	forval x=2003/2016 {
		cap drop rest0_`x'
		egen     rest0_`x' = rowmax(rest53_`x' rest56_`x' rest57_`x')
		cap drop rest1_`x'
		egen     rest1_`x' = rowmax(rest41_`x' rest44_`x' rest47_`x' rest54_`x' rest55_`x' rest58_`x' rest59_`x' rest60_`x')
		cap drop rest2_`x'
		egen     rest2_`x' = rowmax(rest0_`x' rest1_`x')
	}
	
	cap drop restance_nfc
	egen restance_nfc = rowmax(rest2_2003 rest2_2004 rest2_2005 rest2_2006 rest2_2007 rest2_2011 rest2_2012 rest2_2013 rest2_2014 rest2_2015 rest2_2016)
	
	* Generate financial outcome dummies
	gen lico_dum_1 = (bankakt_10ya<persindk_10ya/12) if !mi(bankakt_10ya) & !mi(persindk_10ya) 
		replace lico_dum_1 = 0 if mi(lico_dum_1)
	gen lico_dum_2 = (max_rente_10ya>.30) if !mi(max_rente_10ya) 
		replace lico_dum_1 = 0 if mi(lico_dum_2)

	gen debt_dum1 = (bankgaeld_10ya>0) if !mi(bankgaeld_10ya)
		replace debt_dum1 = 0 if mi(debt_dum1)
	gen debt_dum2 = (bankgaeld_10ya>2*persindk_10ya) if !mi(bankgaeld_10ya) & !mi(persindk_10ya)
		replace debt_dum2 = 0 if mi(debt_dum2)

	gen stock = (kursakt_10ya+oblakt_2015p_10ya>1500)  if !mi(kursakt_10ya)
	replace stock = 0 if mi(stock)
	
	* Generate unemployment dummies
	cap drop  unemp_dum*
	gen unemp_dum1 = (unemp1_10ya>3) if !mi(unemp1_10ya)
	gen unemp_dum2 = unemp2_10ya
	gen unemp_dum3 = (cum_unemp_1_9ya>6) if !mi(cum_unemp_1_9ya)
	
	
	* Save
	save "$data\analyse_del_3"
	
********************************************************************************

**********
* PART 2 *
**********

	use "$temp\kot_population", clear
	
	rename kotnr_opt kotnr
	
	sort year kotnr
	
	merge m:1 year kotnr using "$temp/link_kotnr_udd_niveau_field"
	drop if _merge==2
	drop _merge
	drop kot_navn // udd_niveau
	sort udd
	merge m:1 udd using "$temp/link_udd_stem"
	drop if _merge==2
	drop _merge
	drop udd_field2 udd_field3 udd_field4 stem jura huaa educ welf medi sosc
	
	rename kotnr 		kotnr_opt
	rename udd 			udd_opt
	rename udd_navn 	udd_navn_opt
	rename oeko 		oeko_opt
	rename year 		yoa 
	rename udd_niveau	udd_niveau_opt

	sort pnr yoa

	merge 1:m pnr using "$temp\registerdata_edit2"

	keep if _merge==3
	drop _merge

	sort pnr year

	* Indicate level of current and highest completed education
	tostring udd_n, replace format(%04.0f)
	replace udd_n="" if udd_n=="."
	replace udd = udd_n if mi(udd)
	sort udd
	merge m:1 udd using "$temp\udd_niveau_field"
	drop if _merge==2
	drop _merge
	
	sort pnr year
	drop audd
	tostring hfaudd, gen(audd) format(%04.0f)
	replace audd="" if mi(hfaudd)
	replace audd=audd[_n-1] if mi(audd) & pnr==pnr[_n-1] & !mi(audd[_n-1])
	label var audd "Højest fuldførte uddannelse"
	sort audd
	merge m:1 audd using "$temp\audd_niveau_field"
	drop if _merge==2
	drop _merge
	drop udd_n hfaudd
	
	sort audd
	merge m:1 audd using "$temp\link_audd_stem"
	drop if _merge==2
	drop _merge
	
	sort pnr year
	
	* Years since application
	gen time=year-yoa
	label var time "Years since application"
	
	* Current education prior to YOA
	count if time<0 & !mi(time) & ( ///
		udd_niveau=="Ph.d. og forskeruddannelser"  | ///
		udd_niveau=="Korte videregående uddannelser"  | ///
		udd_niveau=="Lange videregående uddannelser"  | ///
		udd_niveau=="Mellemlange videregående uddannelser / Bachelorer")
		
	* Keep relevant variables
	keep pnr year gpa cut_first opt_prio no_offer num_of_app kotnr_opt kot_navn_opt udd_opt udd_navn_opt oeko_opt rest* max_rente audd inc_rank* birthyear age sex audd_niveau_far audd_niveau_mor audd_navn audd_niveau audd_field2 audd_field3 audd_field4 stem oeko jura huaa educ welf medi sosc yoa time gaeld_til_off_2015p blb_rest* arb_sektorkode sektor udd_niveau_opt

	order pnr year yoa time

	* Highest completed education
	foreach y in stem oeko jura huaa educ welf medi sosc audd audd_field4 audd_field3 audd_field2 {
		gen `y'_10ya = `y' if time==10
		replace `y'_10ya = `y'_10ya[_n-1] if mi(`y'_10ya) & pnr==pnr[_n-1]
	}
	
	gsort pnr - year
	
	foreach y in stem oeko jura huaa educ welf medi sosc audd audd_field4 audd_field3 audd_field2 {
		replace `y'_10ya = `y'_10ya[_n-1] if mi(`y'_10ya) & pnr==pnr[_n-1]
	}
	
	sort pnr year
	
	gen 	kand_10ya = 0 if time == 10 & !mi(audd_niveau)
	replace kand_10ya = 1 if time == 10 & audd_niveau=="Lange videregående uddannelser"
	gen 	bach_10ya = 0 if time == 10 & !mi(audd_niveau)
	replace bach_10ya = 1 if time == 10 & audd_niveau=="Mellemlange videregående uddannelser / Bachelorer"
		
	gen 	audd_niveau_10ya = audd_niveau if time == 10
		
	replace kand_10ya = kand_10ya[_n-1] if mi(kand_10ya) & pnr==pnr[_n-1]
	replace bach_10ya = bach_10ya[_n-1] if mi(bach_10ya) & pnr==pnr[_n-1]
	replace audd_niveau_10ya = audd_niveau_10ya[_n-1] if mi(audd_niveau_10ya) & pnr==pnr[_n-1]
	
	gsort pnr - year
	
	replace kand_10ya = kand_10ya[_n-1] if mi(kand_10ya) & pnr==pnr[_n-1]
	replace bach_10ya = bach_10ya[_n-1] if mi(bach_10ya) & pnr==pnr[_n-1]
	replace audd_niveau_10ya = audd_niveau_10ya[_n-1] if mi(audd_niveau_10ya) & pnr==pnr[_n-1]

	sort pnr year

	* Income
	gen temp = inc_rank if time==10
	bysort pnr: egen inc_rank_10ya = max(temp)
	drop temp 
	
	gen temp = inc_rank if year==2016
	bysort pnr: egen inc_rank_2016 = max(temp)
	drop temp 
	
	drop inc_rank
		
	* Default and delinquency
	merge 1:1 pnr year using "$raw/irte_edit"	
	gen in_irte=(_merge==3)
	drop if _merge==2
	drop _merge

	sort pnr year

	by pnr: egen in_irte_10_all_temp = max(in_irte) if time>9 & !mi(time)
	by pnr: egen in_irte_10_all      = max (in_irte_10_all_temp)
	drop in_irte_10_all_temp

	egen rest_temp = rowmax(rest41 rest44 rest47 rest53 rest55 rest56 rest57 rest58 rest59 rest60)
	by pnr: egen temp = max(rest_temp) if time>9 & !mi(time) & year<2017
	by pnr: egen rest8_10_all = max(temp)
	label var    rest8_10_all "Restance 10-23 år efter YOA"
	drop temp 
	
	by pnr: egen temp = max(rest_temp) if year==2016
	by pnr: egen rest8_2016 = max(temp)
	label var    rest8_2016 "Restance 2016"
	drop temp rest_temp
	
	egen blb_temp  = rowtotal(blb_rest41 blb_rest44 blb_rest47 blb_rest53 blb_rest55 blb_rest56 blb_rest57 blb_rest58 blb_rest59 blb_rest60), missing
	gen temp=blb_temp if year==2016
	bysort pnr: egen blb_rest8_2016=max(temp)
	drop temp blb_temp

	* Marginal interest rate
	by pnr: egen temp = mean(max_rente) if time>9 & time<13
	by pnr: egen gns_max_rente_10_12ya = max(temp)
	label var    gns_max_rente_10_12ya "Gns. marginalrente 10-12 år efter YOA"
	drop temp
	
	by pnr: egen temp = mean(max_rente) if time>6 & time<10
	by pnr: egen gns_max_rente_7_9ya = max(temp)
	label var    gns_max_rente_7_9ya "Gns. marginalrente 7-9 år efter YOA"
	drop temp
	
	by pnr: egen temp = mean(max_rente) if time==10
	by pnr: egen max_rente_10ya = max(temp)
	label var    max_rente_10ya "Marginalrente 10 år efter YOA"
	drop temp
	
	by pnr: egen temp = mean(max_rente) if year==2016
	by pnr: egen max_rente_2016 = max(temp)
	label var    max_rente_2016 "Marginalrente 2016"
	drop temp
	
	* Age at application
	gen alder_yoa_temp=age if time==0
	by pnr: egen alder_yoa=max(alder_yoa_temp)
	drop alder_yoa_temp
	label var alder_yoa "Alder YOA"

	gen alder_yoa_1=(alder_yoa<20)
	gen alder_yoa_2=(alder_yoa==20 | alder_yoa==21)
	gen alder_yoa_3=(alder_yoa==22 | alder_yoa==23)
	gen alder_yoa_4=(alder_yoa==24 | alder_yoa==25)
	gen alder_yoa_5=(alder_yoa>25)

	forval x=1/5 {
		replace alder_yoa_`x'=. if mi(alder_yoa)
	}

	label var alder_yoa_1 "Age < 20"
	label var alder_yoa_2 "Age 20-21"
	label var alder_yoa_3 "Age 22-23"
	label var alder_yoa_4 "Age 24-25"
	label var alder_yoa_5 "Age > 25"
	
	* Completed Field of Study
	gen 	audd_n = . 
	replace audd_n = 1  if stem_10ya == 1
	replace audd_n = 2  if oeko_10ya == 1 
	replace audd_n = 3  if jura_10ya == 1
	replace audd_n = 4  if huaa_10ya == 1
	replace audd_n = 5  if educ_10ya == 1
	replace audd_n = 6  if welf_10ya == 1
	replace audd_n = 7  if medi_10ya == 1
	replace audd_n = 8  if sosc_10ya == 1

	* Sector
	gen 	priv_sec = .
	replace priv_sec = 1 if sektor == "1"
	replace priv_sec = 0 if sektor == "2" |  sektor == "3"
	replace priv_sec = 1 if arb_sektorkode == 11 // Statslige ikke-finansilelle...
	replace priv_sec = 1 if arb_sektorkode == 12 // Kommunale ikke-finansielle...
	replace priv_sec = 1 if arb_sektorkode == 14 // Statsligt ejede ikke-finansielle...
	replace priv_sec = 1 if arb_sektorkode == 15 // Kommunalt ejede ikke-finansielle...
	replace priv_sec = 1 if arb_sektorkode == 16 // Regionsejede ikke finansielle...
	replace priv_sec = 1 if arb_sektorkode == 18 // Private ikke-finansielle...
	replace priv_sec = 1 if arb_sektorkode == 21 // Centralbanken
	replace priv_sec = 1 if arb_sektorkode == 27 // Offentlige pengeinstitutter...
	replace priv_sec = 1 if arb_sektorkode == 28 // Private pengeinstitutter...
	replace priv_sec = 1 if arb_sektorkode == 38 // Private investeringsforeninger...
	replace priv_sec = 1 if arb_sektorkode == 41 // Andre offentlige finansielle...
	replace priv_sec = 1 if arb_sektorkode == 42 // Andre private finansielle..
	replace priv_sec = 1 if arb_sektorkode == 48 // Private finansielle...
	replace priv_sec = 1 if arb_sektorkode == 51 // Offentlige koncerntilknyttede...
	replace priv_sec = 1 if arb_sektorkode == 52 // Private koncerntilknyttede...
	replace priv_sec = 1 if arb_sektorkode == 57 // Offentlige forsikringsselskaber
	replace priv_sec = 1 if arb_sektorkode == 58 // Private forsikringsselskaber
	replace priv_sec = 1 if arb_sektorkode == 62 // Private pensionskasser
	replace priv_sec = 0 if arb_sektorkode == 71 // Statslig forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 72 // Statslig forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 74 // Regionernes forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 75 // Regionernes forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 76 // Kommunal forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 77 // Kommunal forvaltning...
	replace priv_sec = 0 if arb_sektorkode == 79 // Sociale kasser og fonde
	replace priv_sec = 1 if arb_sektorkode == 81 // Personligt ejede virksomheder
	*replace priv_sec =  if arb_sektorkode == 89 // Non-profit...
	*replace priv_sec =  if arb_sektorkode == 91 // Rest of the world...
	*replace priv_sec =  if arb_sektorkode == 99 // Ikke oplyst
	
	replace priv_sec=. if time!=10
	replace priv_sec=priv_sec[_n-1] if pnr==pnr[_n-1] & mi(priv_sec)
	gsort pnr - year
	replace priv_sec=priv_sec[_n-1] if pnr==pnr[_n-1] & mi(priv_sec)
	sort pnr year

	* Keep relevant variables and collapse
	drop year rest_all* rest41* rest42* rest44* rest47* rest52* rest53* rest54* rest55* rest56* rest57* rest58* rest59* rest60* rest64* rest66* max_rente audd audd_navn audd_niveau audd_field2 audd_field3 audd_field4 stem oeko jura huaa educ welf medi sosc in_irte blb_rest41* blb_rest42* blb_rest44* blb_rest47* blb_rest52* blb_rest53* blb_rest54* blb_rest55* blb_rest56* blb_rest57* blb_rest58* blb_rest59* blb_rest60* blb_rest64* blb_rest66* arb_sektorkode sektor 

	keep if time==0
	drop time
	duplicates report pnr

	gen 	restance = rest8_10_all
	sum restance
	replace restance = 0 if mi(restance) & in_irte_10_all==1
	sum restance

	*Save
	save "$data/analyse_first_time_applicants"

**********
* PART 3 *
**********

	use "$data/analyse_first_time_applicants", clear
	keep udd_opt restance yoa
	drop if mi(restance) | mi(udd_opt)
	sort udd_opt yoa
	bysort udd_opt yoa: egen n = count(restance)
	drop if n<20
	drop n
	bysort udd_opt yoa: egen mean_restance_udd_yoa = mean(restance)
	sum mean_restance_udd_yoa, d
	replace mean_restance_udd_yoa=r(p1)  if mean_restance_udd_yoa<r(p1)
	replace mean_restance_udd_yoa=r(p99) if mean_restance_udd_yoa>r(p99)
	keep mean_restance_udd_yoa udd yoa
	duplicates drop
	duplicates report udd yoa
	save "$temp/mean_restance_udd_yoa"
	
	use "$data\analyse_del_3", clear
	
	rename kotnr1 kotnr 
	rename yoa year
	sort kotnr year
	merge m:1 kotnr year using "$temp/link_kotnr_udd_niveau_field"
	drop if _merge==2
	drop _merge
	drop kot_navn udd_navn udd_niveau udd_field2 udd_field3 udd_field4
	rename kotnr kotnr1 
	rename udd udd1
	rename kotnr2 kotnr 
	sort kotnr year
	merge m:1 kotnr year using "$temp/link_kotnr_udd_niveau_field"
	drop if _merge==2
	drop _merge
	drop kot_navn udd_navn udd_niveau udd_field2 udd_field3 udd_field4
	rename kotnr kotnr2
	rename udd udd2
	rename year yoa

	rename udd1 udd_opt
	sort udd_opt yoa
	merge m:1 udd_opt yoa using "$temp/mean_restance_udd_yoa"
	drop if _merge==2
	drop _merge	
	rename mean_restance_udd_yoa mean_restance_udd_yoa1
	rename udd_opt udd1 
	rename udd2 udd_opt
	sort udd_opt yoa
	merge m:1 udd_opt yoa using "$temp/mean_restance_udd_yoa"
	drop if _merge==2
	drop _merge	
	rename mean_restance_udd_yoa mean_restance_udd_yoa2
	rename udd_opt udd2
	
	* Add information on institution and generate dummies
	rename kotnr1 kotnr
	rename yoa year 
	sort year kotnr
	merge m:1 year kotnr using "$raw/kot_adgangskvotienter"
	drop if _merge==2
	drop kot_navn adgangskvotient _merge
	rename kot_institution kot_institution1
	rename kotnr kotnr1
	rename kotnr2 kotnr
	sort year kotnr
	merge m:1 year kotnr using "$raw/kot_adgangskvotienter"
	drop if _merge==2
	drop kot_navn adgangskvotient _merge
	rename kot_institution kot_institution2
	rename year yoa
	rename kotnr kotnr2
	sort pnr

	tab kot_institution1 if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if
	tab kot_institution2 if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if

	cap drop ku
	gen ku = 0 if optaget1==1 | optaget2==1
	replace ku = 1 if (optaget1==1 & kot_institution1=="Københavns Universitet") | (optaget2==1 & kot_institution2=="Københavns Universitet")

	cap drop cbs
	gen cbs = 0 if optaget1==1 | optaget2==1
	replace cbs = 1 if (optaget1==1 & kot_institution1=="Handelshøjskolen I København") | (optaget2==1 & kot_institution2=="Handelshøjskolen I København")
	replace cbs = 1 if (optaget1==1 & kot_institution1=="Handelshøjskolen i København") | (optaget2==1 & kot_institution2=="Handelshøjskolen i København")

	cap drop au
	gen au = 0 if optaget1==1 | optaget2==1
	replace au = 1 if (optaget1==1 & kot_institution1=="Aarhus Universitet") | (optaget2==1 & kot_institution2=="Aarhus Universitet")
	replace au = 1 if (optaget1==1 & kot_institution1=="Århus Universitet") | (optaget2==1 & kot_institution2=="Århus Universitet")

	cap drop bss
	gen bss = 0 if optaget1==1 | optaget2==1
	replace bss = 1 if (optaget1==1 & kot_institution1=="Handelshøjskolen I Århus") | (optaget2==1 & kot_institution2=="Handelshøjskolen I Århus")
	replace bss = 1 if (optaget1==1 & kot_institution1=="Handelshøjskolen i Århus") | (optaget2==1 & kot_institution2=="Handelshøjskolen i Århus")
	
	save "$data\analyse_del_3", replace
